0

I have the two following contracts: tryContract and testData. The testData contract aims at fetching data from the tryContract contract utilizing its get_attributes function. Before doing any fetching, assume I fill in the map in the tryContract contract using the following inputs:

["0x0","0x1","0x2","0x3","0x4","0x5","0x6","0x7","0x8","0x9"]

pragma solidity ^0.4.21;
contract tryContract {
    mapping (address=>bytes32[10]) Map;

    function fill_map(bytes32[10] _attributes) public  {
            Map[msg.sender] = _attributes;
    }

    function get_attributes(address _id) view public returns (bytes32[10]){
      return Map[_id];
    }
}

contract testData {
    address Address;
    //function takes address of user as input
    function get_address (address _Address) constant{   
        Address = _Address;
    } 

    bytes32[10] Data;
    //function takes the address of previous contract as input
    function fetch(address _try_Adress) public view returns (bytes32[10]) {
        tryContract r = tryContract(_try_Adress);
        Data = r.get_attributes(Address);
        return (Data);
    }
}

After running the code and trying to fetch the data stored, the output I receive is incorrect. The output is:

["0x0","0x0","0x0","0x0","0x0","0x0","0x0","0x0","0x0","0x0"]

Why does it not recognize the data that had been previously stored in the map?

  • Your get_attributes function is returning staff[_id] but your mapping object is called Map. Are you trying to return Map[_id]? – machine-uprising Jun 4 '18 at 14:42
  • Sorry that was a typo. I fixed it now. – blockchainenth Jun 4 '18 at 14:43
  • Did this fix your output? – machine-uprising Jun 4 '18 at 14:49
  • No, but I think I found the fix. The function "get_address" was declared as constant. Once that was removed, the data was fetched properly. – blockchainenth Jun 4 '18 at 14:54
1

There is a bit of confusion in the testData. The function get_address has been defined as constant but is changing the state (so it should be a set instead) and the Address is used into the fetch to get the data.

function fetch(address _try_Adress) public view returns (bytes32[10]) {
    tryContract r = tryContract(_try_Adress);
    Data = r.get_attributes(Address);
    return (Data);
}

you can use this which is equivalent and you don't need the get_address (in case will work properly) to be called before the fetch

function fetch(address _try_Adress) public view returns (bytes32[10]) {
    tryContract r = tryContract(_try_Adress);

    return r.get_attributes(msg.sender);
}

another way might be to fix the get_address function (which I called set_address in this case)

function set_address (address _Address) public {   
    Address = _Address;
} 

function fetch(address _try_Adress) public view returns (bytes32[10]) {
    tryContract r = tryContract(_try_Adress);

    return r.get_attributes(Address);
}

I suggest the first approach since you will avoid one call.

0

Remove constant from get_address function.

function get_address (address _Address){   
        Address = _Address;
    }

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.