简体   繁体   中英

web3 react contract interaction

I am having some issues when calling a contract via form with React and Web3 component.

In my contract, I have a public function called GetDomainInfo which takes the domain as a parameter. You can view the contract here: 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.

EDIT: rouDomains is the async load data from 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:

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:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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