using web3 javascript library how do you get the current value of a public property. e.g.
contract MyContract {
address public owner;
...
}
Here's a snippet of the abi:
[{
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
...
]
I've tried several methods, but to no avail:
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var contract = new web3.eth.Contract([...], "0x1234...");
// These don't work:
var owner = contract.owner;
console.log(owner); // "undefined"
contract.methods.owner.call(function(error, result) {
console.log("This doesn't get called");
});
contract.methods.owner(function(error, result) {
// Displays an error to console:
// Error: Invalid number of parameters for "owner". Got 1 expected 0!
});
var x = contract.methods.owner();
console.log(x); // Displays the contract object below:
The last line displays the contract object. Here's a snippet:
{ call: { [Function: bound _executeMethod] request: [Function: bound
_executeMethod] },
send: { [Function: bound _executeMethod] request: [Function: bound
_executeMethod] },
encodeABI: [Function: bound _encodeMethodABI],
estimateGas: [Function: bound _executeMethod],
_method:
{ constant: true,
inputs: [],
name: 'owner',
outputs: [ [Object] ],
payable: false,
type: 'function',
signature: '0x8da5cb5b' },
_parent:
Contract {
// etc...
EDIT: Using version 1.0.0-beta.4 (later packages don't install properly with dependency lerna). Docs: http://web3js.readthedocs.io/en/1.0/web3-eth-contract.html
Best Answer
This code is correct:
myContract.methods.owner().call().then(console.log);
The error is a bug, and will be fixed in the next version.