Error: invalid string value (arg="_name", coderType="string", value=null)
Error: invalid string value (arg="_name", coderType="string", value=null)
我正在尝试将个人信息从 UI 解析为智能合约,我遇到的问题似乎是我遵循的示例解析了 int,但我不确定要按顺序更改什么解析字符串?此代码只是试图获取玩家姓名和生日。
这是我的智能合约代码:
pragma solidity 0.6.6;
contract Athlete_contract4{
string public playerName;
string public playerBirthday;
string public playerAddress;
string public playerNationality;
function setData(string memory _name, string memory _birthday) public{
playerName = _name;
playerBirthday = _birthday;
// playerAddress = _address;
// playerNationality = _nationality;
}
}
还有我的 UI 代码:
<html>
<body>
<div>
<h4>Athlete Details</h4>
<input type="text" id="name" placeholder="Name">
<br><br>
<input type="date" id="birthday">
<br><br>
<input type="text" id="address" placeholder="Address">
<br><br>
<input type="text" id="nationality" placeholder="Nationality">
<br><br>
<button id='submit'>Submit</button>
</div>
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.36/dist/web3.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" crossorigin="anonymous"></script>
<script>
var contract;
$(document).ready(function () {
web3 = new Web3(window.web3.currentProvider);
var address = "0xD9190906543d08725f5d523A1CEd83Fcde4f1F28";
var abi = [
{
"inputs": [],
"name": "playerAddress",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "playerBirthday",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "playerName",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "playerNationality",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_name",
"type": "string"
},
{
"internalType": "string",
"name": "_birthday",
"type": "string"
}
],
"name": "setData",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
];
contract = new web3.eth.Contract(abi, address);
})
$('#submit').click(function()
{
var name = 0;
name = parseInt($('#name').val());
var birthday = 0;
birthday = parseInt($('#birthday').val());
web3.eth.getAccounts().then(function(accounts){
var acc = accounts[0];
return contract.methods.setData(name, birthday).send({from: acc});
}).then(function(tx)
{
console.log(tx);
}).catch(function(tx)
{
console.log(tx);
})
});
</script>
</body>
</html>
当我在 visual studio 代码中使用 http-server
将其部署到本地主机时,出现此错误:
是否有必须使用的解析字符串代码?或者我只是在某处遗漏了一行代码?
在您的提交函数中 - 您将“姓名”和“生日”字段转换为整数(其中 solidity 需要一个字符串)。
尝试删除 parseInt 函数。
name = parseInt($('#name').val());
至
name = $('#name').val();
我正在尝试将个人信息从 UI 解析为智能合约,我遇到的问题似乎是我遵循的示例解析了 int,但我不确定要按顺序更改什么解析字符串?此代码只是试图获取玩家姓名和生日。
这是我的智能合约代码:
pragma solidity 0.6.6;
contract Athlete_contract4{
string public playerName;
string public playerBirthday;
string public playerAddress;
string public playerNationality;
function setData(string memory _name, string memory _birthday) public{
playerName = _name;
playerBirthday = _birthday;
// playerAddress = _address;
// playerNationality = _nationality;
}
}
还有我的 UI 代码:
<html>
<body>
<div>
<h4>Athlete Details</h4>
<input type="text" id="name" placeholder="Name">
<br><br>
<input type="date" id="birthday">
<br><br>
<input type="text" id="address" placeholder="Address">
<br><br>
<input type="text" id="nationality" placeholder="Nationality">
<br><br>
<button id='submit'>Submit</button>
</div>
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.36/dist/web3.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" crossorigin="anonymous"></script>
<script>
var contract;
$(document).ready(function () {
web3 = new Web3(window.web3.currentProvider);
var address = "0xD9190906543d08725f5d523A1CEd83Fcde4f1F28";
var abi = [
{
"inputs": [],
"name": "playerAddress",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "playerBirthday",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "playerName",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "playerNationality",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_name",
"type": "string"
},
{
"internalType": "string",
"name": "_birthday",
"type": "string"
}
],
"name": "setData",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
];
contract = new web3.eth.Contract(abi, address);
})
$('#submit').click(function()
{
var name = 0;
name = parseInt($('#name').val());
var birthday = 0;
birthday = parseInt($('#birthday').val());
web3.eth.getAccounts().then(function(accounts){
var acc = accounts[0];
return contract.methods.setData(name, birthday).send({from: acc});
}).then(function(tx)
{
console.log(tx);
}).catch(function(tx)
{
console.log(tx);
})
});
</script>
</body>
</html>
当我在 visual studio 代码中使用 http-server
将其部署到本地主机时,出现此错误:
是否有必须使用的解析字符串代码?或者我只是在某处遗漏了一行代码?
在您的提交函数中 - 您将“姓名”和“生日”字段转换为整数(其中 solidity 需要一个字符串)。
尝试删除 parseInt 函数。
name = parseInt($('#name').val());
至
name = $('#name').val();