Skip to main content
3 of 4
Improvement
BokkyPooBah
  • 40.4k
  • 14
  • 123
  • 193

Try the following:

function getUsersCount() public constant returns(uint) {
    return users.length;
}

function getUser(uint index) public constant returns(uint, string, address) {
    return (users[index].idNum, users[index].name, users[index].userAddress);
}

You should then be able use the statements:

> var numberOfUsers = contract.usersCount();
undefined
> numberOfUsers
5
> var user0 = contract.users(0)
[123, "User 123", "0xbeefee3ad17cd9aca6fd5343a53a513685c7e0ed"]
> var user1 = contract.getUser(1)
[234, "User 234", "0xbeefee3ad17cd9aca6fd5343a53a513685c7e0ed"]
> var lastUser = arrayStruct.users(numberOfUsers-1)
undefined
> lastUser
[567, "User 567", "0xbeefee3ad17cd9aca6fd5343a53a513685c7e0ed"]

And responding to the comment by Nikhil M, the statement below always returns 0 for me. I don't know if it is a design feature or a bug.

> var users = contract.users.length
undefined
> users
0

The call to contract.usersCount(), contract.users(x) and contract.getUser() don't change the blockchain data and don't cost gas. From Greeter,

Run the Greeter In order to call your bot, just type the following command in your terminal:

greeter.greet();

Since this call changes nothing on the blockchain, it returns instantly and without any gas cost. You should see it return your greeting:

BokkyPooBah
  • 40.4k
  • 14
  • 123
  • 193