0

Simple contract:

contract ex {
   address[] users;
   function addUser() public {
        users.push(msg.sender);
   }

   function returnUsers() constant returns (address[]) {
       return users;
   }
}

In web3js I have the contract instance exInstance:

var abi = { /* some ABI */ };
var exInstance = web3.eth.contract(abi).at(someaddress);

function getUsers(){
   let users = exInstance.returnUsers.call();
}

And the result is that users is a null. Tried async:

function getUsers(){
   exInstance.returnUsers.call(function(error, users)
      /// some actions
}

But still null.

Am I doing something wrong or I just should iterate it in web3js and in contract just use something like returnUsers(uint i) constant returns (address) { return users[i]; } ?

  • Is an up to date version of your contract deployed? – 0xcaff Jul 22 '17 at 17:07
  • oh,u r right, updated the contract but forgot to update the abi.. thank you! – porfavorite Jul 22 '17 at 18:10
2

It is possible to return dynamically sized arrays from external transactions or calls. An external call/transaction is initiated by something outside of the contract code running in the EVM. (Source)

Can you return an array or a string from a solidity function call?

Yes. See array_receiver_and_returner.sol.

What is problematic, though, is returning any variably-sized data (e.g. a variably-sized array like uint[]) from a fuction called from within Solidity. This is a limitation of the EVM and will be solved with the next protocol update.

Returning variably-sized data as part of an external transaction or call is fine.

I'm not sure what is wrong with your code. It works for me in remix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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