【问题标题】:web3 react contract interactionweb3 react 合约交互
【发布时间】:2020-08-25 11:21:32
【问题描述】:

我在通过带有 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);
}

【问题讨论】:

  • 显示完整代码。

标签: reactjs blockchain ethereum smartcontracts web3


【解决方案1】:

答案是我在下面忘记了一个“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})

愚蠢的菜鸟错误:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-19
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 2021-09-27
    • 2023-01-19
    相关资源
    最近更新 更多