2

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);

2 Answers 2

6

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

require(userInfo[msg.sender].userAddress != address(0));
1
  • 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. Apr 25, 2018 at 17:01
2

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.

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.