So I have a smart contract, in my code, I have an array and i am pushing to it using a particular method call addToUser, from the code below Contract Example{ string[] users; function addToUser(_user) public { users.push(_user); } } from my understanding when I call the addToUser method above and I push records to it. users is stored in the contract storage(which I am thinking of, as a database). my confusion is, how do I see all the users. like how do I access the database to see all the records and what happens when I redeploy the contract?

1 Answer 1

  1. Declare string[] public users;
  2. Add function getNumOfUsers() public view returns (uint256) {return users.length;}
  3. In your Truffle test, do something like this inside an async function:
const numOfUsers = await myContract.getNumOfUsers();
for (let i = 0; i < numOfUsers; i++)
    console.log(await myContract.users(i));
  • is there any other way i can do this? given that my contract is already deployed Commented Feb 10, 2020 at 8:31
  • @Alcantara: There are several ways. You haven't posted a single piece of code explaining what you want to do with that information, so I suggested one option. You also haven't specified why you are looking for other ways. Note that we're not here to guess what your goal here, so please explain it in your question in an explicit manner that would leave no room for guessing! Commented Feb 10, 2020 at 8:37
  • my question is not really a code issue, my code works. what I want is for example in my nodejs project, I have a database where I save everything to, and I can access this database. my confusion is how do I check the record stored in my deployed contract. using a tool or something. thanks for your help Commented Feb 10, 2020 at 9:49
  • @Alcantara: Just as I showed you! Commented Feb 10, 2020 at 10:29

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.