0

I am working on a Dapp and facing some issues. I have created a contract which has a function as

function getId() public view returns (uint8){
 return getMyId[msg.sender];
}

I am calling the same in the front end and desire that id should be returned from the contract and be stored in the variable. The code in App.js is as follows

const myId = await this.state.dstock.methods.getId.call({ from: this.state.account }); 
console.log("myId",myId);

This is returning something unexpected

enter image description here

Kindly provide a suitable solution for this.

0

I think you're missing parenthesis in the method call. The following should work:

const myId = await this.state.dstock.methods.getId().call({ from: this.state.account });
5
  • this worked, thanks a lot! – Shrey Jun 9 at 10:56
  • Can you also please explain why I was getting an object returned in the former case and not any error? – Shrey Jun 9 at 10:57
  • @Shrey not sure if I could pin it down. web3.methods has many ways for how it lets you access your desired method. For example, you could try this.state.dstock.methods["getId()"]().call({ from: this.state.account }) by method with parameter, or with the abi-encoded form. I think if you print both this.state.dstock.methods.getId() and this.state.dstock.methods.getId and compare, you'll see how .call() exists on both layers, as web3 is very flexible and doesn't confine you to the way you should formulate the method call. – scorpion9979 Jun 9 at 12:35
  • Furthermore, I believe you might also be able to make a correct method call by using: await this.state.dstock.methods.getId.call().call({ from: this.state.account }); – scorpion9979 Jun 9 at 12:36
  • Not sure on the latter, so please try it and come back with some feedback. – scorpion9979 Jun 9 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.