0

I am using react as my front end to call functions from smart contract .here is my contract including the function i use to populate the map.

 this is the front end where i call the functions. However, after calling the first function which populates the map I then call the second function to get the username using the same userid but i get an empty string.

After testing , i figured that after populating the map , the values inside the map just restore their original values or something similar and so when calling other functions they see an empty map. any help is appreciated as this is my first blockchain based project . thank you

0

The problem is by the use of .call() it will execute the function as a view, it will not make permanent changes.

The correct way to make changes is to use .send() instead.

contract.methods.addUser(...params).send({ from: userAddress });
| improve this answer | |
  • thank you , i realy new to this i know it is not the smartest question but i try my best. thank you for your help . – Nour Aljzeery Mar 23 at 10:49
0

One thing you could try is to update the line below

this.state.contract.methods.add(0, this.state.username, this.state.email, this.state.password).call()

To this:

this.state.contract.methods.add(0, web3.utils.toHex(this.state.username), web3.utils.toHex(this.state.email), web3.utils.toHex(this.state.password)).call({from:"Your Account Address", gasPrice:web3.utils.toHex(10000000000), gas:web3.utils.toHex(6000000)}) //be sure to assign your preferred gasPrice and gas if it's not a testnet. 

As "addUser" function will change the state of the smart contract, a "send" method needs to be used instead of "call". "call" can't alter a smart contract state.

I've also attached the web3 JS library reference here:

https://web3js.readthedocs.io/en/v1.2.6/web3-eth-contract.html#methods-mymethod-send

Please let me know if it doesn't help.

Cheers,

| improve this answer | |
  • thank you , i realy new to this i know it is not the smartest question but i try my best. thank you for your help . – Nour Aljzeery Mar 23 at 10:49
  • No problems Nour. Let me know if you have further questions. Cheers. – AlexW Mar 23 at 21:44

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.