I have the following in my contract:

struct familyMember {
        bool isParent;
        string name;
        uint balance;
        bool active;
        uint spendLimit;
        address accountNumber;

    mapping (address => familyMember) familyMembers;
    address[] public familyMemberAccounts;

I have the following function I want to use to check if a mapping exists for a given struct:

function checkFamilyMember(address _address) private view returns (bool) {
        bool familyMemberExists = false;
        if (bytes(familyMembers[_address].name).length != 0) {
            familyMemberExists = true;
        return familyMemberExists;

I don't think this is working as intended but am not sure how I can debug. I would normally use a console.log() but I dont think I can do this in Remix.

Is this a correct way to check if mapped struct exists or is there a better way? I did not see any recent answers (I am using version 0.8.0). This answer is 4 years old, so I wanted to know if there might be a new way.

  • Nothing has changed. If you want to known if a mapping entry exists you should add a field to the struct or reuse an existing field for such purpose.
    – Ismael
    May 12, 2021 at 4:12
  • 1
  • Thanks @Ismael, yes, the link you mentioned was helpful. I had already reviewed it and mentioned it in my post. I modeled my code based on that answer as well. I just wanted to see if there have been any new ways to handle this in the last 4 years since solidity is changing so much. Thanks!
    – joselvelez
    May 12, 2021 at 5:13
  • I'd suggest to not use a string field for such purpose, using a fixed size type like bool, uintNNN, bytesNN will work better.
    – Ismael
    May 12, 2021 at 5:24
  • Thanks, I will use a bool instead of a string.
    – joselvelez
    May 12, 2021 at 14:17


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.