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.

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

1 Answer 1

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];
}
4
  • 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. Jan 17, 2020 at 0:01
  • 1
    @PaulRazvanBerg But you cannot return a struct that has a mapping from a public/external function.
    – Ismael
    Jan 17, 2020 at 4:00
  • 1
    Thank you so much, this is the way to do the thing. Jan 17, 2020 at 8:22
  • @Ismael interesting, I thought that v2 covered that. Jan 17, 2020 at 12:34

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.