1

In my function, while using my mapping to reference to an array, I keep getting the error message below:

TypeError: Type struct etherGifter.CryptoGift storage ref[] storage ref is not 
implicitly convertible to expected type struct etherGifter.CryptoGift memory.
--> contracts/gifter4.sol:100:9:
|
100 | CryptoGift memory validGifts = myGifts[msg.sender];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Can someone kindly help me resolve this?

Find below my snippets of my code:

enum Status { pendingGifting, giftGifted }

struct CryptoGift {
    Status  GiftStatus;
}

mapping(address => CryptoGift[]) private myGifts;

function viewMyGifts() public view returns ( CryptoGift[] memory ) {
    CryptoGift memory validGifts = myGifts[msg.sender];

    require( validGifts.GiftStatus != Status.giftGifted, "You have no active gifts!" );

    return myGifts[msg.sender];
}

1 Answer 1

1

In this code the problem is that you're converting a CryptoGift[] (struct's array) in a single instance of CryptoGift (struct) at this line:

CryptoGift memory validGifts = myGifts[msg.sender];

To resolve it, you can modify the code in this way:

function viewMyGifts() public view returns ( CryptoGift[] memory) {
        CryptoGift[] memory validGifts = myGifts[msg.sender];
        for(uint i = 0; i < validGifts.length; i++){
            // In this line you'll have access to a single instance of struct contained in `myGifts` mapping 
        }
        return myGifts[msg.sender];
    } 
3
  • Thank you for the prompt response Kerry99. I do not want to access a single instance of the struct, but rather a list of ALL the instances stored in the array!
    – SirBT
    Commented Mar 1, 2022 at 11:33
  • Therefore don't make the for cycle on an CryptoGift[] array. Please note, if you don't iterate the single instances contained in an array, you cannot access to each variable declared in the struct. Commented Mar 1, 2022 at 11:38
  • Thanks Kerry99, the desired return result is an array that I can later iterate through on the front end UI.
    – SirBT
    Commented Mar 1, 2022 at 12:19

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.