1

I am unable to reference struct elements which are inside mapping.

Mapping:

    struct VoterData {
        string iD;
        address voterEthAddress;
    }

    // One to Many mapping; one Eth Address can have many VoterData
    mapping(address => VoterData[]) public votersMapping;

Here is the code I am trying to reference the mapping:

emit messageEvent("Voter Address: ",votersMapping[localAddress].voterEthAddress );

Error

TypeError: Member "voterEthAddress" not found or not visible after argument-dependent lookup in struct Voter.VoterData storage ref[] storage ref.
--> contracts/Voter.sol:64:53:
|
64 | emit messageEvent("Voter Address: ",votersMapping[localAddress].voterEthAddress );
|

1 Answer 1

0

You have a map of addresses pointing to an array of structs.

You are missing the index of your array. (Maybe they can vote on different proposals that's why you have that secondary array and the index is the proposal ID? Although that seems inefficient)

mapping(address => VoterData[]) public votersMapping;
emit messageEvent("Voter Address: ",votersMapping[localAddress][index].voterEthAddress );

Should work, but I imagine you don't want that index there, and you want addresses directly to point to the voterdata you should modify your map to not be pointing to an array.

mapping(address => VoterData) public votersMapping;

Best of luck!

3

Your Answer

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

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