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?
Add a comment
|
1 Answer
- Declare
string[] public users;
- Add
function getNumOfUsers() public view returns (uint256) {return users.length;}
- 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
-