I have a function in Solidity:

address[] contracts;

function getContractsCount() constant returns (uint256) {
      return contracts.length;

in Remix it's work perfect. I get 1, it's right, but when I call the function from javascript:

import FactoryContract from '../build/contracts/Factory.json';

const selectContractInstance = (contractBuild) => {
  return new Promise(res => {
    const myContract = contract(contractBuild);
      .then(instance => res(instance));

var factoryContract = await selectContractInstance(FactoryContract);    

var contractCount = await factoryContract.getContractsCount.call().toNumber();

I get wrong number - 0.

How to return uint from Solidity to JavaScript from contract function

[UPDATE] ---------

Solidity Code

address[] public contracts;
 TokenFactory public newContract;

function createContract(uint _totalSupply, string _name, string _symbol, uint _decimal) payable returns(uint256 t) {
  newContract = new TokenFactory(_totalSupply, _name, _symbol, _decimal);
  return (contracts.length);
} //return 1, it's right

function getContractsCount() constant returns (uint256 length) {
  return (contracts.length);
} // after that the function return 0, it's wrong!

JavaScript Code not change

  • I add gas limit for function in JavaScript and it resolve problem. Anyway thanks for answers. :) Commented Aug 11, 2017 at 10:50

1 Answer 1


I think the contract address or ABI is not correctly set in your case because I tested and it works perfectly.

Please check if correct contract address and ABI is updated in your javascript file.



As you asked, I am attaching my code:

Solidity Code:

pragma solidity ^0.4.0;

contract testContract {
    uint256 num1;

    address[] contracts = [0x36eaf79c12e96a3dc6f53426c, 0xf235aa56dd96bda02acfb361e];

    function getContractsCount() constant returns (uint256) {
          return contracts.length;


jQuery code:

    function contractInvocation() {
        var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
        var abi = <ABI>;
        var testContract = web3.eth.contract(abi);
        var contractInstance = testContract.at('<Contract Address>');
        var y = contractInstance.getContractsCount.call({from:web3.eth.accounts[0]}).c[0];
  • thanks for you answer, now it look like: address[] contracts; function getContractsCountOne() constant returns (uint256) { contracts.push(newAddress); return contracts.length; } function getContractsCountTwo() constant returns (uint256) { return contracts.length; } From JavaScript, I call getContractsCountOne() and get 1, then i just call getContractsCountTwo() and I get 0, but why? contract address and ABI set correct. Commented Aug 11, 2017 at 7:11
  • Can i see your code, please? Commented Aug 11, 2017 at 7:34
  • I sae your code in comment, that doesn't seems to be correct. If your function is constant function getContractsCountOne() constant returns (uint256) {, it won't be able to do any write operations which you are doing (Pushing and element to array).
    – Abhishek
    Commented Aug 11, 2017 at 8:00
  • Yes your right. But sorry, it copy-paste, in original code constant does not exist. The function set like payable. Commented Aug 11, 2017 at 8:05
  • Can you share your exact complete code in the question?
    – Abhishek
    Commented Aug 11, 2017 at 8:08

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.