1

I'm quite new at programming and I'm facing an issue where i'm not able to acces a public variable from a contract.

In the contract, I've declared following public variable:

uint public numFoodInfos;

In JS I try to call this variable:

contractAdressF2F = "...";
ABIArrayF2F = ...;
dataF2F = "...";
myContractF2F = web3.eth.contract(ABIArrayF2F).at(contractAdressF2F);

myContractF2F.numFoodInfos.call(function (error, result) {
    if (!error)
        console.log(result.toNumber())
    else
        console.log(error);
});

However, this returns 'null'.

Anyone sees the problem here? I read that public variables should generate a getter function automatically.

FYI, I'm using the meteor framework. Other custom functions of the same contract do work...

1 Answer 1

4

Try calling that variable using parenthesis like this -

myContractF2F.numFoodInfos().call(function (error, result) {
    if (!error)
        console.log(result.toNumber())
    else
        console.log(error);
});

The reason why we need to add parenthesis is explained here - https://solidity.readthedocs.io/en/latest/contracts.html#getter-functions (When you make a state variable public, the compiler automatically generates a getter function as described in the documentation)

1
  • I've added the parenthesis as you suggested, but then I get the following error: inpage.js:217 Uncaught Error: The MetaMask Web3 object does not support synchronous methods like eth_call without a callback parameter. See github.com/MetaMask/faq/blob/master/… for details. Apr 27, 2017 at 19:44

Your Answer

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

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