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!

MrRav3n
  • 5
  • 2

1 Answers1

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.

Mikhail Vladimirov
  • 7,313
  • 1
  • 23
  • 38