1

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]; } ?

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

1 Answer 1

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.

1
  • 1
    In the remix, it works fine but string[] memory returns null in js if we are using web3 Commented Nov 1, 2019 at 2:56

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.