1

I have a problem when executing the code

HelloWorld.deployed().balance.call().then(console.log)

This error introduced: Cannot read property 'call' of undefined This problem is similar to this thread but it does not help on my case.

enter image description here Thanks for your help!

My code is from this turorial

contract HelloWorld {
  address public owner;
  mapping (address => uint) balances;
  function HelloWorld() {
    owner = msg.sender;
    balances[owner] = 1000;
  }
  function transfer(address _to, uint _value) returns (bool success) {
    if (balances[msg.sender] < _value) {
      return false;
    }
    balances[msg.sender] -= _value;
    balances[_to] += _value;
    return true;
  }
  function getBalance(address _user) constant returns(uint _balance) {
    return balances[_user];
  }
}
1
  • Could you provide the code of your contract HelloWorld?
    – PierrickM
    Commented Mar 27, 2018 at 15:40

1 Answer 1

0

You don't have a method called "balance" but "getBalance" with an address argument "_user", so you need to call it this way:

HelloWorld.deployed().getBalance.call('YOUR_ADDRESS')
16
  • I have tried this one. Unfortunately, it did not work. I added my code that you can better help Commented Mar 28, 2018 at 8:04
  • Test with: HelloWorld.deployed().getBalance.call('YOUR_ADDRESS') or something like this should work: HelloWorld.deployed().getBalance.call(web3.eth.accounts[0])
    – PierrickM
    Commented Mar 28, 2018 at 12:01
  • I have tried this one too but it didn't work :((( Commented Mar 28, 2018 at 13:15
  • What do you got when you run : HelloWorld.deployed() ?
    – PierrickM
    Commented Mar 28, 2018 at 13:46
  • It seems that HelloWorld.deployed() works well. I got something like this TruffleContract { constructor: { [Function: TruffleContract] _static_methods: { setProvider: [Function: setProvider], new: [Function: new], ... Commented Mar 28, 2018 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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