How i can check if particular item exists in the Mapping list?

struct user {
    address userAddress;
    uint[] entries;

mapping (address => user) public userInfo;

I found such solution here but this solution is not working:

require(userInfo[msg.sender].length == 0);

If you want to know if a user exists try the following:

require(userInfo[msg.sender].userAddress != address(0));
| improve this answer | |
  • This answer is the most efficient if your are checking for the existence of an address property. In the case of uint or most other types, you will most likely need to add a flag to your struct as mentioned below. – Zack McGinnis Apr 25 '18 at 17:01

You need to declare a flag in a struct

struct user{ address userAddress; uint[] entries; bool isExist; } mapping (address => user) public userInfo; require(userInfo[address].isExist)

Assign isExist to true while inserting a record.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.