Just chiming in for the benefit of others who come across this.
@Majd is right about the logic error. Even so, there are additional errors in this.
The for loop is an anti-pattern. Its mere existence is not a problem, but the fact that there could be no upper limit to the number of users, therefore the number of iterations, therefore the gas cost of this is a well-known anti-pattern. More here: https://blog.b9lab.com/getting-loopy-with-solidity-1d51794622ad
It doesn't make sense to store a username, password and userid on the chain. This information would be visible to everyone, and it adds cost. It's not necessary to do so because Ethereum transaction signing ensures that all everyone who uses the contract is reliably authenticated.
Instead, map their addresses to user profiles.
If there is a requirement to collect additional information or grant permission to join the system, then a bit for isUser, is appropriate. In most cases that would be unwanted de-anonymization and unnecessarily intrusive. It may be perfectly fine to assume that all users exist. That would reduce the struct to a single property - some users are admins.
pragma solidity 0.5.1;
contract UserExample {
struct UserStruct {
// string username; // use the address
// string password; // don't store this on chain
// address userid;
bool isUser; // only needed if there is user initialization
bool isAdmin;
}
mapping(address => UserStruct) public userStructs;
function isUser(address user) public view returns(bool) {
return userStructs[user].isUser;
}
}
See also: Are there well-solved and simple storage patterns for Solidity?
Hope it helps.
elseandreturn falseoutside (at the end of) the loop. Also, your understanding ofmappingseems completely wrong. How exactly are you hoping to iterate it the way you do??? There is obviously no point in amappinghere. You should be using an array. – goodvibration Jun 08 '19 at 10:32mappingwouldn't even need aforloop. The key should be your inputaddress userid, and you can return the result in a single attempt. For this purpose, simply add abool isValidto your structure, and set ittruewhenever you add a user. Then, in your function, simply returnusers[userid].isValid. – goodvibration Jun 08 '19 at 10:39