2

I deploy a simple token contract "MetaCoin through web3.js as follows:

var Web3 = require('web3');
var web3 = new Web3(Web3.givenProvider || 'http://localhost:8545');
const abi = [...];
var MetaCoin = new web3.eth.Contract(abi, "0x77e82a433403f2d3273f391414b1eafe7e792624");

I can call a function 'getBalance()' of the contract and get the result like this:

MetaCoin.methods.getBalance("0x77e82a433403f2d3273f391414b1eafe7e792624").call().then(function(result){ console.log(result); });

But how can I call a state like 'address public creator' defined in the contract?

1 Answer 1

1

When setting variables to public it automatically creates a getter method that can be used to query the variable.

contract Foo {
    uint public bar;
}

=>

myContractInstance.bar.call().then(function(result){ console.log(result); });
4
  • I know. In my contract, I define: contract MetaCoin { address public creator; } then I call: MetaCoin.creator.call().then(function(result){ console.log(result); }); I get error: TypeError: Cannot read property 'call' of undefined`
    – Anderson
    Aug 3, 2017 at 1:32
  • past in the abi
    – jayD
    Aug 3, 2017 at 3:58
  • I am I have latest abi
    – Anderson
    Aug 3, 2017 at 9:31
  • But thank you, I find MetaCoin.methods.creator().call().then(function(result){ console.log(result); }); will get the value
    – Anderson
    Aug 3, 2017 at 9:33

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.