3

This question already has an answer here:

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?

marked as duplicate by Ismael, Rob Hitchens - B9lab, Tjaden Hess, Nicolas Massart, Badr Bellaj Jul 3 '17 at 23:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

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.

  • You can use console.log(balance.toNumber()); – Ismael Jul 3 '17 at 5:48
  • This isn't really a change in truffle. It's just that now they are using the latest version of web3 which uses Promises. – Michael Smith Dec 12 '18 at 3:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.