1

I have the following:

mapping (address => mapping (address => uint[])) public allowed;

function approve (address _to, uint256 tokenId) external {
   allowed [msg.sender][_to].push(tokenId);
}

So an address is mapped to an address, which then can bring back an array of uints. However, when testing this code it only seems to work the first time, so the uint array only ever has one item (whatever the tokenId parameter was the first time I called the approve function). No error is given on Etherscan. What could the issue be here?

2

i tried the following Code in Remix:

pragma solidity ^0.4.6;

contract test {

    mapping (address => mapping (address => uint[])) public allowed;

    function approve (address _to, uint256 tokenId) external {
       allowed [msg.sender][_to].push(tokenId);
    }

    function getArray(address testAddress) constant returns (uint[]) {
        return allowed[msg.sender][testAddress];
    }
}

When using getArray it returns the expected array so the function approve obviously works fine.

When I try the automatically generated getter function provided by solidity I need to specify the index of the element in the array. If I just provide the first and the second address it returns only the first element (index 0). Try the code above and use the automatically generated getter function with the following arguments:

first_address,second_address,index

By modifying index you should be able to view all elements in the array.

Hope it helps.

  • OK, code is fine – ZhouW Feb 1 '18 at 9:03

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.