4

Good day.

I have a struct object called HomeWork

HomeWork[] public homeworks;

struct HomeWork{
    uint256 assignId;
    string description;
    mapping(address=>uint256)handedAt;
    mapping(address=>bool)passedOrFailed;

};

As you can see, this object contains nested mappings inside.

I'm trying to interact with this object using web3.

contract = web3.eth.contract([ABI]).at("0x0..");
var homeworks = contract.homeworks();

But when I'm examining specific instance, I can't get any information about the mappings:

[>homeworks(2)]

[>BigNumber, "this is description for item number 3"] * there should be 2 mappings here *

That's it. the mappings aren't there at all.

Any idea what might be the problem?

1 Answer 1

4

It's not possible for mappings. In your case you can only access the assignId and description of your struct doing contract.homeworks(index);

If you want to "fix" it you should write your custom getter function.

Here is what the doc says :

Accessor Functions The compiler automatically creates accessor functions for all public state variables. The contract given below will have a function called data that does not take any arguments and returns a uint, the value of the state variable data. The initialization of state variables can be done at declaration.

The accessor functions have external visibility. If the symbol is accessed internally (i.e. without this.), it is a state variable and if it is accessed externally (i.e. with this.), it is a function.

contract test {
   uint public data = 42;
}

The next example is a bit more complex:

contract complex {
  struct Data { uint a; bytes3 b; mapping(uint => uint) map; }
  mapping(uint => mapping(bool => Data[])) public data;
}

It will generate a function of the following form:

function data(uint arg1, bool arg2, uint arg3) returns (uint a, bytes3 b)

{
  a = data[arg1][arg2][arg3].a;
  b = data[arg1][arg2][arg3].b;
}

Note that the mapping in the struct is omitted because there is no good way to provide the key for the mapping.

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.