1

I'm kinda new to Solidity, sorry for this in advance. I'm building a Dapp and cannot understand how to get a mapping or an array from a Struct. This is the struct that have inside a two mappings and one array.

struct Something {
    uint256 var;
    string[] array;
}

This is the result printed in the console:

{
    var: "17"
}

As you can see, the call returns only level one attributes.

How to get that array from the struct?

Thanks in advance.

  • Sounds like contract.methods._tasks(i)._freelancerArray[index] should work – Lauri Peltonen Jan 16 at 19:44
1

It is not possible in solidity for a function to return a variable of type mapping.

The default getter of a public struct variable will not return dynamic data types like mapping or dynamic array.

To access data from dynamic field inside an struct you have to implement your own getter.

function getTaskFreelancer(uint256 taskId, uint256 idx) public view returns (addres) {
    require(_tasks[taskId]._freelancerArray.length > 0, "Invalid freelancer index");
    return _tasks[taskId]._freelancerArray[idx];
}
| improve this answer | |
  • 1
    Returning structs is possible with Abi Encoder v2, but anyone reading this should be warned: as of January 2020, v2 is highly experimental and should not be used in production. – Paul Razvan Berg Jan 17 at 0:01
  • 1
    @PaulRazvanBerg But you cannot return a struct that has a mapping from a public/external function. – Ismael Jan 17 at 4:00
  • 1
    Thank you so much, this is the way to do the thing. – Irimia Cosmin Iulian Jan 17 at 8:22
  • @Ismael interesting, I thought that v2 covered that. – Paul Razvan Berg Jan 17 at 12:34

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.