'Web3 Cannot get value of a public variable from contract

Solidity code:

pragma solidity ^0.5.8;


 contract remainNew{

    address public owner = msg.sender;
    uint public cubeCount = 0;

    modifier onlyBy(address _account){
    require(
        msg.sender == _account,
        "Sender not authorized"
    );
    _;
}

struct aCube{
    uint cubeID;
    string owner;
    string ipfsHash;
    string fileName;
    string fileSize;
    string description;
    bool isEncrypted;
    string time;
}

mapping(uint => aCube) public cubes;


function generateCube(string memory _o,string memory _h, string memory _n, string memory _s,
 string memory _d, bool  _i, string memory _t) public {
    cubeCount++;
    cubes[cubeCount] = aCube(cubeCount,_o,_h,_n,_s,_d,_i,_t);
   }

function setHash(string memory _newHash, uint _ID) public onlyBy(owner){
    cubes[_ID].ipfsHash = _newHash;
  }

}

abi:

"abi": [
{
  "constant": true,
  "inputs": [
    {
      "name": "",
      "type": "uint256"
    }
  ],
  "name": "cubes",
  "outputs": [
    {
      "name": "cubeID",
      "type": "uint256"
    },
    {
      "name": "owner",
      "type": "string"
    },
    {
      "name": "ipfsHash",
      "type": "string"
    },
    {
      "name": "fileName",
      "type": "string"
    },
    {
      "name": "fileSize",
      "type": "string"
    },
    {
      "name": "description",
      "type": "string"
    },
    {
      "name": "isEncrypted",
      "type": "bool"
    },
    {
      "name": "time",
      "type": "string"
    }
  ],
  "payable": false,
  "stateMutability": "view",
  "type": "function"
},
{
  "constant": true,
  "inputs": [],
  "name": "cubeCount",
  "outputs": [
    {
      "name": "",
      "type": "uint256"
    }
  ],
  "payable": false,
  "stateMutability": "view",
  "type": "function"
},
{
  "constant": true,
  "inputs": [],
  "name": "owner",
  "outputs": [
    {
      "name": "",
      "type": "address"
    }
  ],
  "payable": false,
  "stateMutability": "view",
  "type": "function"
},
{
  "constant": false,
  "inputs": [
    {
      "name": "_o",
      "type": "string"
    },
    {
      "name": "_h",
      "type": "string"
    },
    {
      "name": "_n",
      "type": "string"
    },
    {
      "name": "_s",
      "type": "string"
    },
    {
      "name": "_d",
      "type": "string"
    },
    {
      "name": "_i",
      "type": "bool"
    },
    {
      "name": "_t",
      "type": "string"
    }
  ],
  "name": "generateCube",
  "outputs": [],
  "payable": false,
  "stateMutability": "nonpayable",
  "type": "function"
},
{
  "constant": false,
  "inputs": [
    {
      "name": "_newHash",
      "type": "string"
    },
    {
      "name": "_ID",
      "type": "uint256"
    }
  ],
  "name": "setHash",
  "outputs": [],
  "payable": false,
  "stateMutability": "nonpayable",
  "type": "function"
}
 ]

web3:

 try{

const fileCount = await contract.methods.cubeCount().call();

console.log(fileCount);

}catch(error){
    console.log(error);
    return;
}

error:

Error: Returned values aren't valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced. at ABICoder.push../node_modules/web3-eth-abi/src/index.js.ABICoder.decodeParameters (index.js:226) at Contract.push../node_modules/web3-eth-contract/src/index.js.Contract._decodeMethodReturn (index.js:471)

web3 version is: 1.2.4

Having problem retrieving value from a public variable. Although I use await here, but it seems like it is not returning a promise. I can use send() function without error. Need help with this, thanks.

if I use const fileCount = await contract.methods.cubeCount.call(); instead I will get an object:

{arguments: Array(0), call: ƒ, send: ƒ, encodeABI: ƒ, estimateGas: ƒ, …} arguments: [] call: ƒ () encodeABI: ƒ () estimateGas: ƒ () send: ƒ () _ethAccounts: Accounts {_requestManager: RequestManager, givenProvider: Proxy, providers: {…}, _provider: Proxy, …} _method: {constant: true, inputs: Array(0), name: "cubeCount", outputs: Array(1), payable: false, …} _parent: Contract {_requestManager: RequestManager, givenProvider: Proxy, providers: {…}, _provider: Proxy, …} proto: Object



Solution 1:[1]

If you are new like me make sure you have deployed latest contact :-).

In my case I needed this.

truffle migrate --reset

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rajan Lagah