[Ethereum] Problem with Truffle Console: Cannot read property ‘call’ of undefined

soliditytestrpctruffle

I´m having problem with the basic HelloWorld example coded in Solidity and managed with truffle with testrpc.

pragma solidity ^0.4.2;

contract HelloWorld {
uint public balance;

function HelloWorld() {
balance = 1000;
}
}

enter image description here

>  truffle(development)>
> HelloWorld.deployed().balance.call().then(console.log) TypeError:
> Cannot read property 'call' of undefined
>     at evalmachine.<anonymous>:1:30
>     at ContextifyScript.Script.runInContext (vm.js:35:29)
>     at Object.exports.runInContext (vm.js:67:17)
>     at TruffleInterpreter.interpret (c:\Users\whha\AppData\Roaming\npm\node_modules\truffle\lib\repl.js:128:17)
>     at bound (domain.js:280:14)
>     at REPLServer.runBound [as eval] (domain.js:293:12)
>     at REPLServer.<anonymous> (repl.js:545:10)
>     at emitOne (events.js:96:13)
>     at REPLServer.emit (events.js:188:7)
>     at REPLServer.Interface._onLine (readline.js:239:10) truffle(development)>

The problem looked similar to
this thread, but it does not solve my problem:

The contract is properly deployed and has a valid JSON document, I really don´t understand why I cannot access its members. The only one that return value is a constructor.

truffle(development)> HelloWorld.deployed().constructor
[Function: Object]

Any ideas??

Best Answer

If you are running the latest version of Truffle (v.3.x), the way to call a function or variable in your contract has evolved a little bit according to the Upgrading documentation

In Truffle 2.0, your contract abstractions managed your networks in a naive way, and added constructs like a "default network" that opened up the possibility of using the wrong network artifacts and deployed addresses at the wrong time. This provided a fancy and easy to use syntax – i.e., MyContract.deployed().myFunction(...) – but it left your code open to errors. Truffle 3.0 changes this syntax, where .deployed() is now thennable, like a promise (see example below). Similarly, this makes your contract abstractions seamlessly integrate with EIP 190, Ethereum's package management standard. All of this is for the better, but it means you'll have some changes to make all across the board.

So in your case, if you want to call a function, you have to do this:

HelloWorld.deployed().then(function(instance) {
    return instance.balance.call();
}).then(function(balance) {
    console.log(balance);
});

Please refer to the latest documentation for more details.