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];
  }
}
  • Could you provide the code of your contract HelloWorld? – PierrickM Mar 27 '18 at 15:40
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')
  • I have tried this one. Unfortunately, it did not work. I added my code that you can better help – Van Minh Nhon TRUONG Mar 28 '18 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 Mar 28 '18 at 12:01
  • I have tried this one too but it didn't work :((( – Van Minh Nhon TRUONG Mar 28 '18 at 13:15
  • What do you got when you run : HelloWorld.deployed() ? – PierrickM Mar 28 '18 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], ... – Van Minh Nhon TRUONG Mar 28 '18 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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