当通过带有React和Web3组件的表单调用合同时,我遇到了一些问题。
在我的合同中,我有一个名为GetDomainInfo的公共函数,该函数将域作为参数。您可以在此处查看合同:https://ropsten.etherscan.io/address/0x756ad7f7c22e7c04a845bd8a138e455a1bc95f6f
问题是我的组件获得了表单值,但是在合同中使用它时,出现错误:
Uncaught TypeError: Cannot read property 'GetDomainInfo' of undefined
代码:
GetInfo = (event) => {
event.preventDefault();
console.log(this.state.value);
const response = this.state.rouDomains.method.GetDomainInfo(this.state.value).send({from: this.state.account})
.once('receipt', (receipt) => {
console.log("receipt: ", receipt);
console.log(response);
})
}
数据到达console.log(this.state.value),我可以看到它。
编辑:rouDomains是来自web3的异步加载数据。
async loadData() {
const web3 = new Web3(Web3.givenProvider || "http://localhost:8545");
const network = await web3.eth.net.getNetworkType();
this.setState({network});
//Fetch account
const accounts = await web3.eth.getAccounts();
this.setState({ account: accounts[0]});
//Load the contract
const rouDomains = new web3.eth.Contract(ROU_TLD, ROU_TLD_ADDRESS);
this.setState({ rouDomains });
console.log("ROU: ", rouDomains);
}
答案是,我在下面的方法中忘记了's':
const response = this.state.rouDomains.method.GetDomainInfo(this.state.value).send({from: this.state.account})
const response = this.state.rouDomains.methods.GetDomainInfo(this.state.value).send({from: this.state.account})
愚蠢的菜鸟错误:)