4

I am new to Solidity. I am facing an issue in a simple operation of storing and retrieving data from mapping (uint=> address).

I have defined a simple contract:

contract test {
    mapping (uint => address) public testmap;

    function add_to_map  (uint _key, address _val) public{
        testmap[_key] = _val;
    }
    function get_from_map(uint _key) returns (address){
        return testmap[_key];
    }   
}

In html, I am trying to do:

function createTestContract(){
    var test = testContract.at("0xc383dfb5fc71ff1bb2bbadb812229681fb7a8e3c");
    test.add_to_map(1, "0x12ca4a043753cd5537af99ad314a299962238ed2");       
    console.log('Key: 1'  + ' address: ' + test.get_from_map(1));
}

As output I am expecting address - 0x12ca4a043753cd5537af99ad314a299962238ed2 which I am stored against key 1. However, it is returning some random address in the console log.

Key: 1 address: 0x07824f2f2a330dd2e260437af299800175dc2642a63c71a5f824cee54eae2264

I am using private chain on AlethZero. Appreciate if anyone can help.

1 Answer 1

7

You need to specify constant so that you get the return value of a call:

function get_from_map(uint _key) constant returns (address).

Without constant, test.get_from_map(1) is a sendTransaction which always returns a transaction hash. See What is the difference between a transaction and a call?

1
  • Thank you @eth. It is clear now and it worked. Many thanks.
    – jrocky
    Commented May 21, 2016 at 10:05

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.