[Ethereum] truffle always says “.the_function is not a function”


I'm trying to do my first basic smart contract, following this popular HelloWorld tutorial.

However whenever I get to the part where I run a function in the truffle console, it is always telling me ".function_name is not a function".

For example, calling the default truffle MetaCoin getBalance function :

truffle(development)> MetaCoin.deployed().getBalance( web3.eth.accounts[0] )
TypeError: MetaCoin.deployed(...).getBalance is not a function

Am I calling the function incorrectly?
How should I be calling the .getBalance function in the MetaCoin example?

Best Answer

OK - this is essentially a duplicate - however I'm going to leave my question here because I've asked it very differently and I think others may ask the question this way too.

The answer is that truffle has changed the mechanism for calling functions since that video was made, the deployed() method returns a promise so you need to wait for that.
Here is the way to call the getBalance function now :

truffle(development)> MetaCoin.deployed().then( function(instance) { return instance.getBalance.call( web3.eth.accounts[0] ) }).then(function(balance) { console.log(balance) })

{ [String: '10000'] s: 1, e: 4, c: [ 10000 ] }

Source : this answer.