There are some similar posts to this but none seem to be able to solve my problem.

I have a contract deployed with Truffle running with testrpc.

I want to access the value of a public uint variable named gameStatus defined in the deployed contract game variable as such:

uint public gameStatus = 23;

I have found two ways to do this:

game.gameStatus(function(err, res){
    document.getElementById('amt').innerText = res;

And using the then operator from Promises.

    document.getElementById('tableamt').innerText = result;

The first method never affects the value of 'tableamt' in the HTML. And the second method always yields a value of '0' in the HTML.

The contract in both cases deploys without errors and web3 does not complain.

Any help is much appreciated.


  • 1
    Have you tried web3.eth.getCode(contractAddress) and confirmed the contract is indeed deployed? Jan 17, 2017 at 16:41
  • Trying to do that gives me the following error: Uncaught Error: The MetaMask Web3 object does not support synchronous methods like eth_getCode without a callback parameter. However when I do game.deployed() I get this: Contract BettingOver : () address : "0x7881c3a33335a8cceb04b86131f4bf43db26dbd8" allEvents : () betEnd : () bettingEnd : () bettingStart : () So it looks like the variables are not being set during deploy? Jan 17, 2017 at 17:20
  • Then that should do web3.eth.getCode(contractAddress, function(err, code) { console.log(code);}) Jan 17, 2017 at 17:24
  • web3.eth.getCode("0x7881c3a33335a8cceb04b86131f4bf43db26dbd8", function(err, code){ console.log(code)}); undefined VM245:1 0x Jan 17, 2017 at 17:33
  • 0x means there is no code at the address. Of course your "functions" return 0. Jan 17, 2017 at 17:50

4 Answers 4


To get public variable value you need to use call. It is evaluated directly on local node without sending transaction to the blockchain. See also this great answer.

So, you need to call it that way:

game.gameStatus.call(function(err, res){
    document.getElementById('amt').innerText = res;

It's different now. You should use contractName.methods.varName().call(callback) to access state variables in web3 1.0.0. Solidity compiler automatically creates getters for public state variables.

To answer your question, the method call should be:

game.methods.gameStatus().call(function(err, res){
    //do something with res here
    console.log(res); //for example

A simple promise works just fine.

game.gameStatus().then(function (result) {

game is the loaded contact, which in my case, I get with another promise.

App.contracts.GameContract.deployed().then(function (game) {
    return game.winningNumbers();
}).then(function (result) {


(await game.gameStatus()).toString()

or this

game.gameStatus().then((x) => x.toString())

works for me.

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.