6

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?

0

1 Answer 1

4

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.

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

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