I am trying to parse a persons information from a UI to a smart contract, the problem I seem to be having is the example I followed parses int’s and I’m not sure what to change in order to parse strings across? This code is just trying to get the player name and birthday.
Here is my smart contract code:
JavaScript
x
20
20
1
pragma solidity 0.6.6;
2
3
contract Athlete_contract4{
4
string public playerName;
5
string public playerBirthday;
6
string public playerAddress;
7
string public playerNationality;
8
9
10
11
function setData(string memory _name, string memory _birthday) public{
12
playerName = _name;
13
playerBirthday = _birthday;
14
// playerAddress = _address;
15
// playerNationality = _nationality;
16
17
}
18
19
}
20
And my code for my UI:
JavaScript
1
140
140
1
<html>
2
3
<body>
4
<div>
5
<h4>Athlete Details</h4>
6
<input type="text" id="name" placeholder="Name">
7
<br><br>
8
<input type="date" id="birthday">
9
<br><br>
10
<input type="text" id="address" placeholder="Address">
11
<br><br>
12
<input type="text" id="nationality" placeholder="Nationality">
13
<br><br>
14
15
<button id='submit'>Submit</button>
16
17
</div>
18
19
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.36/dist/web3.min.js"></script>
20
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" crossorigin="anonymous"></script>
21
22
<script>
23
var contract;
24
25
$(document).ready(function () {
26
web3 = new Web3(window.web3.currentProvider);
27
28
var address = "0xD9190906543d08725f5d523A1CEd83Fcde4f1F28";
29
var abi = [
30
{
31
"inputs": [],
32
"name": "playerAddress",
33
"outputs": [
34
{
35
"internalType": "string",
36
"name": "",
37
"type": "string"
38
}
39
],
40
"stateMutability": "view",
41
"type": "function"
42
},
43
{
44
"inputs": [],
45
"name": "playerBirthday",
46
"outputs": [
47
{
48
"internalType": "string",
49
"name": "",
50
"type": "string"
51
}
52
],
53
"stateMutability": "view",
54
"type": "function"
55
},
56
{
57
"inputs": [],
58
"name": "playerName",
59
"outputs": [
60
{
61
"internalType": "string",
62
"name": "",
63
"type": "string"
64
}
65
],
66
"stateMutability": "view",
67
"type": "function"
68
},
69
{
70
"inputs": [],
71
"name": "playerNationality",
72
"outputs": [
73
{
74
"internalType": "string",
75
"name": "",
76
"type": "string"
77
}
78
],
79
"stateMutability": "view",
80
"type": "function"
81
},
82
{
83
"inputs": [
84
{
85
"internalType": "string",
86
"name": "_name",
87
"type": "string"
88
},
89
{
90
"internalType": "string",
91
"name": "_birthday",
92
"type": "string"
93
}
94
],
95
"name": "setData",
96
"outputs": [],
97
"stateMutability": "nonpayable",
98
"type": "function"
99
}
100
];
101
102
103
contract = new web3.eth.Contract(abi, address);
104
105
106
})
107
108
$('#submit').click(function()
109
{
110
111
var name = 0;
112
name = parseInt($('#name').val());
113
114
var birthday = 0;
115
birthday = parseInt($('#birthday').val());
116
117
118
web3.eth.getAccounts().then(function(accounts){
119
120
var acc = accounts[0];
121
return contract.methods.setData(name, birthday).send({from: acc});
122
}).then(function(tx)
123
{
124
console.log(tx);
125
}).catch(function(tx)
126
{
127
console.log(tx);
128
})
129
130
131
132
});
133
134
135
</script>
136
137
</body>
138
139
</html>
140
When I deploy this to a local host using http-server
in visual studio code I get this error:
Is there a parse string code that has to be used? Or have I just missed a line of code somewhere?
Advertisement
Answer
In your submit function – you are casting your “name” and “birthday” fields to an integer (where solidity is expecting a string).
Try removing the parseInt function.
JavaScript
1
2
1
name = parseInt($('#name').val());
2
to
JavaScript
1
2
1
name = $('#name').val();
2