1

I have recently started learning solidity. I added a function showMessage() like below:

function showMessage() public view returns(string memory) {
        return "This is a constant";
    }

In the truffle console when I call it helloworld.showMessage() it gives error:

truffle(ganache)> helloworld.showMessage()
evalmachine.<anonymous>:0
helloworld.showMessage()
           ^

Uncaught TypeError: helloworld.showMessage is not a function
    at evalmachine.<anonymous>:0:12
    at sigintHandlersWrap (node:vm:273:12)
    at Script.runInContext (node:vm:140:14)
    at runScript (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:270:1)
    at Console.interpret (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:285:1)
    at bound (node:domain:416:15)
    at REPLServer.runBound [as eval] (node:domain:427:12)
    at REPLServer.onLine (node:repl:834:10)
    at REPLServer.emit (node:events:376:20)
    at REPLServer.EventEmitter.emit (node:domain:470:12)

The method is right there as you can see here the output which is generated if I print helloworld variable:

 },
    events: { allEvents: [Function: bound ] },
    _address: '0xEb1799BA2295143d0A85FF6939c53fEC3ED42eaC',
    _jsonInterface: [ [Object], [Object], [Object] ]
  },
  getMessage: [Function (anonymous)] {
    call: [Function (anonymous)],
    sendTransaction: [Function (anonymous)],
    estimateGas: [Function (anonymous)],
    request: [Function (anonymous)]
  },
  setMessage: [Function (anonymous)] {
    call: [Function (anonymous)],
    sendTransaction: [Function (anonymous)],
    estimateGas: [Function (anonymous)],
    request: [Function (anonymous)]
  },
  showMessage: [Function (anonymous)] {
    call: [Function (anonymous)],
    sendTransaction: [Function (anonymous)],
    estimateGas: [Function (anonymous)],
    request: [Function (anonymous)]
  },
  sendTransaction: [Function (anonymous)],
  send: [Function (anonymous)],
  allEvents: [Function (anonymous)],
  getPastEvents: [Function (anonymous)]
}

Below is my entire contract:

pragma solidity 0.5.16;

contract HelloWorld {
    string private message = "hello world";

    function getMessage() public view returns(string memory) {
        return message;
    }

    function setMessage(string memory newMessage) public {
        message = newMessage;
    }

    function showMessage() public view returns(string memory) {
        return "This is a constant";
    }
}

The rest of the functions work fine.

Update:

NO I was wrong. After updating the contract and rebuilding and migrate, it is saying every function undefined after compiling and deployment. In Truffle console I initiated as:

let helloworld = HelloWorld.deployed(
helloworld.setMessage('mssg')

It worked first time.

Your Answer

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

Browse other questions tagged or ask your own question.