简体   繁体   English

web3 react 合约交互

[英]web3 react contract interaction

I am having some issues when calling a contract via form with React and Web3 component.我在通过带有 React 和 Web3 组件的表单调用合同时遇到了一些问题。

In my contract, I have a public function called GetDomainInfo which takes the domain as a parameter.在我的合同中,我有一个名为 GetDomainInfo 的公共 function,它将域作为参数。 You can view the contract here: https://ropsten.etherscan.io/address/0x756ad7f7c22e7c04a845bd8a138e455a1bc95f6f您可以在此处查看合约: https://ropsten.etherscan.io/address/0x756ad7f7c22e7c04a845bd8a138e455a1bc95f6f

The problem is that my components gets the form value, but when use it in a contract, gives the error:问题是我的组件获得了表单值,但是在合同中使用它时,会出现错误:

Uncaught TypeError: Cannot read property 'GetDomainInfo' of undefined

Code:代码:

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);
})
}

The data arrives in console.log(this.state.value), I can see it.数据到console.log(this.state.value),可以看到。

EDIT: rouDomains is the async load data from web3.编辑: 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);
}

The answer was that I forgot a 's' for method in the below:答案是我在下面忘记了一个 '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})

Stupid rookie mistake:)愚蠢的菜鸟错误:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM