基本上,我的用例是创建注册表格并将其链接到区块链。到目前为止,我的合同已经在Remix中工作(版本0.4.25编译),但是,当我去尝试使用reactJS将数据提交到区块链时,会出现错误
节点错误:{“消息”:“无法从null读取属性” ...“
有人可以帮我吗?
App.js:
onsaveInfo = async (event) =>{
/*var data={
fname:this.state.fname,
lname:this.state.lname,
phone:this.state.phno
}*/
console.log("final data-----",this.state._fname,this.state._lname,this.state._phno);
localStorage.setItem('data',this.state._fname,this.state._lname,this.state._phno);
console.log("1------------",getWeb3);
// getWeb3.then(results=>{
// let web3 = results.payload.web3Instance;
// var callingAddress= web3.currentProvider['publicConfigStore'].getState().selectedAddress;
// console.log("2-----------------",callingAddress);
// })
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
console.log("current provider ------",web3.eth.givenProvider.selectedAddress);
// web3.eth.getAccounts(console.log);
//alert("qwertyuiop");
const contractInstance = new web3.eth.Contract(this.state.contractAbi,this.state.contractAddress);
console.log(contractInstance);
await contractInstance.methods.createRandomUser(this.state._fname,this.state._lname,this.state._phno).call(function(error,result){
if(!error){
console.log("result========",result);
}
});
}
render() {
if (!this.state.web3) {
return <div>Loading Web3, accounts, and contract...</div>;
}
return (
<div className="App">
<div className="col-md-12">
<div className="card">
<label>First Name : </label>
<input type="text" placeholder="firstName" onChange={(event)=>this.setState({_fname:event.target.value})}/>
<br></br>
<label>Last Name : </label>
<input type="text" placeholder="lastName" onChange={(event)=>this.setState({_lname:event.target.value})}/>
<br></br>
</div>
<label>Ph No : </label>
<input type="text" placeholder="phNo" onChange={(event)=>this.setState({_phno:event.target.value})}/>
<br></br>
<label>Role : </label>
<select>
<option >Select</option>
<option value="Driver1" onClick={event=>this.setState({roleName:event.target.value})}>Driver1</option>
<option value="Driver2" onClick={event=>this.setState({roleName:event.target.value})}>Driver2</option>
<option value="Driver3" onClick={event=>this.setState({roleName:event.target.value})}>Driver3</option>
</select>
<br></br>
</div>
<div>
<input type="button" onClick={event=>this.onsaveInfo(event)} value='Save Info'/>
</div>
</div>
);
书面合同:
pragma solidity ^0.4.25;
contract ambFactory {
struct User {
string _fname;
string _lname;
uint _phno;
}
uint phDigits = 10;
uint phModulus = 10 ** phDigits;
User[] public users;
function _setProfile(string _fname, string _lname, uint _phno) private {
users.push(User(_fname, _lname, _phno));
}
/*function _genRandomUser(string _str) private view returns (uint) {
uint rand = uint(keccak256(abi.encodePacked(_str)));
return rand % phModulus;
}*/
function createRandomUser(string _fname, string _lname, uint _phno) public {
//uint randNo = _genRandomUser(_fname);
_setProfile(_fname, _lname, _phno);
}
}
我希望信息会发送到我设置的本地区块链上,并且交易会反映在MetaMask(我正在使用的提供程序)中,但是,不会出现这样的交易确认窗口,并且会得到错误。 / p>