[Ethereum] Truffle CMD Bignumber to number; hex to string

bignumbersoliditytruffle

A function in my contract when called on the truffle develop command line gives output in BigNumber format.
Here is what I wrote on the command line:

instance = contract.at('deployed address')

instance.getAge()

I tried instance.getAge().toNumber() but that throws an error saying that toNumber isn't defined. I checked How to convert BigNumber to Number in Truffle framework? however that doesnt seem to work on command line.

Similary outputs from functions returning bytes32 are in hex.I checked web3 return bytes32 string but web3.utils.toAscii(x) {x is any variable} throws an error

"Cannot read property toAscii of undefined"

I read the stack related to that and that also didn't help.

Best Answer

Contract function calls in Truffle return promises so you have to add a callback to get the response. If getAge() is declared as either constant, pure or view in the Solidity contract you can do instance.getAge().then(age => age.toNumber()) if it doesn't have any of those modifiers then you need to do instance.getAge.call().then(age => age.toNumber())