0

Hi guys i have one really big problem. I have this pice of code

function showInfo(uint _pesel) public onlyMainPerson(msg.sender) returns (string memory, string memory) {
    string memory x = showName(_pesel);
    string memory y = showDate(_pesel);
    return(x,y);
}

function showName(uint _pesel) private view returns( string memory) {
    return(wills[_pesel].name);
}
function showDate(uint _pesel) private view returns( string memory) {
    return(wills[_pesel].date);
}

And i want to use it in my web app, i'm doing it by

 const names = await this.state.contract.methods.showInfo(123).send({from: this.state.account}, (e) => {
        console.log("done")
        console.log(names);
    })
    console.log(names);

But it is not working! It only shows "undefinded". How to solve it? Thanks in advance!

0
1

You have two problems here:

  1. You are executing your function on-chain, thus you cannot catch returned value.
  2. send method returns transaction hash, not function call result.

The simplest way to fix this would be to mark your function as view and execute off-chain via call method:

function showInfo(uint _pesel) public view onlyMainPerson(msg.sender) returns (string memory, string memory) {
  string memory x = showName(_pesel);
  string memory y = showDate(_pesel);
  return(x,y);
}
const names = await this.state.contract.methods.showInfo(123).call(
  {from: this.state.account});
console.log(names);

See documentation for details.

5
  • This attempt at unfolding a confusing topic might help: blog.b9lab.com/… – Rob Hitchens Dec 19 '19 at 17:41
  • Yes but there is a problem because i dont know how to send msg.sender in call function. – MrRav3n Dec 19 '19 at 19:09
  • If i was able to do it would fix my problem ^^ – MrRav3n Dec 19 '19 at 19:51
  • As I said in my answer, see documentation for details: web3js.readthedocs.io/en/v1.2.4/web3-eth-contract.html#id29 . BTW, you may set arbitrary msg.sender address, even address you don't have private key for. – Mikhail Vladimirov Dec 19 '19 at 20:25
  • Ok, thats it what i've been looking for! Thanks! – MrRav3n Dec 20 '19 at 18:06

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