web3 react 合约交互
web3 react contract interaction
我在使用 React 和 Web3 组件通过表单调用合同时遇到了一些问题。
在我的合同中,我有一个名为 GetDomainInfo 的 public 函数,它将域作为参数。您可以在此处查看合同: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})
愚蠢的菜鸟错误:)
我在使用 React 和 Web3 组件通过表单调用合同时遇到了一些问题。
在我的合同中,我有一个名为 GetDomainInfo 的 public 函数,它将域作为参数。您可以在此处查看合同: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})
愚蠢的菜鸟错误:)