I use web3.js and try to take a value of the variable from the smart contract.

My code for it is:

var contract = web3.eth.contract(abi).at(address);

where abi - json array and address - valid working address.

Part of abi json object is

      "constant": true,
      "inputs": [],
      "name": "weiRaised",
      "outputs": [
          "name": "",
          "type": "uint256"
      "payable": false,
      "type": "function"

When I use this code:


I see "weiRaised" in the console.

But if I use


I see 'undefined'.

What I am doing wrong? How Can I get the variable from the smart contract, based on abi and web3.js?

4 Answers 4


After printing stuff on the console, God let me go to sleep with joy =)

var contract = web3.eth.contract(abi).at(address);
async function getContractPublicVariable() {
    const weiRaised = await contract.methods.weiRaised.call().call();

so the idea is contract.methods.VARIABLENAME.call().call();

  • Another idea is contract.methods.VARIABLENAME().call();
    – Stiin
    Commented Feb 6, 2022 at 21:08

Web3.js will automagically map the ABI functions onto the contract object that you get back from web3.eth.contract(abi).at(address).

This means you can access the value of the variables by doing something like:

let weiRaisedValue = contract.weiRaised.call()

  • console.log(contract.weiRaised.call()); - TypeError: contract.weiRaised is undefined
    – Sergei
    Commented Nov 29, 2017 at 19:05
  • 1
    @Sergei Are you using web3 v1.0? In that case it should be contract.methods.weiRaised.call().then(function (res) { console.log(res); }). More info from web3 v1.0 documentation.
    – Ismael
    Commented Nov 30, 2017 at 21:56
  • console.log(web3.version); gives this: Object { api: "0.20.2", node: Getter, getNode: e(), network: Getter, getNetwork: e(), ethereum: Getter, getEthereum: e(), whisper: Getter, getWhisper: e() }
    – Sergei
    Commented Dec 1, 2017 at 23:53
  • your code gives this error to me: TypeError: contract.methods is undefined
    – Sergei
    Commented Dec 1, 2017 at 23:53

I'll give an updated answer as of web3.js 1.6.0 (I used moralis as my provider for web3.js)

import Web3 from 'web3';       
const web3Js = new Web3(Moralis.provider);
            var contract = new web3Js.eth.Contract(ContractJSON.abi, ContractAddress);
            const variable = await contract.methods.variableName.call().call();
  • 1
    The double call() looks odd, it looks better with contract.methods.variableName().call(), using () after the name and a single .call().
    – Ismael
    Commented Feb 3, 2022 at 13:30

2022 WebJs 1.7.3

If anyone struggle with it in 2022, then this is the solution

import Web3 from "web3";

const web3 = new Web3(provider);
const myContract = new web3.eth.Contract(
this.age = await myContract.methods.getAge().call();

This is the generic form. And It is an async call await myContract.methods.getVariableName().call();

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.