Solidity Function – Fixing Invalid Number of Arguments Error

truffleweb3js

I have tested it in remix and the function does work, I also removed inputs just for testing purposes but it seems that it doesn't work anyway.

Here is the function:

struct Message{
    string Message;
    int Vote;
    uint timeSubmit;
    bool isImportant;
    uint senderId;
}
Message[] messageArr; 
function getMessageString() public view returns(string memory){
        return messageArr[2].Message;
    }

I am calling it in web3.js like this:

  function getString() {
        messageInstance.getMessageString(function(error, result){
          if (error) {
              console.log(error)
          }
          else {
        console.log("yes")

      }})
    }

I am using truffle but it keeps giving me this error.

Best Answer

Function getMessageString takes 0 parameters, yet you are calling it with 1 parameter.

Therefore, change this:

messageInstance.getMessageString(function(error, result)

To something like this:

messageInstance.getMessageString().then(function(result)
Related Topic