Explore Courses Blog Tutorials Interview Questions
0 votes
in Blockchain by (4.1k points)

not sure if I should post this on the Ethereum SE but I have a feeling it is more javascript related, so I'll try here:

I have a very simple smart contract that consists of essentially just a getNum function and a setNum function. The smart contract can be viewed here:

I am trying to construct a simple frontend to call it. Essentially, I follow this guide. A working codepen of my frontend (demonstrating the janky functionality) can be found here: As you can see in my codepen, I call my getNum function like:


        function(error, result)
            document.getElementById("target").innerText = result.toNumber();


However, I keep getting "0" as the result. On the other hand, my setNum function is able to post a result to the Ropsten blockchain. However, I notice that both function calls seem to be firing off their callback instantly.

Could anyone help me figure out why the function calls return instantly?


1 Answer

0 votes
by (14.4k points)
edited by

The function calls return instantly because an actual address (in the form of String) is not being sent. Rather, a number is sent. Moreover, you are getting ‘0’ because the address that you are sending is invalid and on top of that it does not exist in the mapping. 

And when this happens, the default value for uint variables are returned. The default value is zero. 

If you want to make sure that only the current user can retrieve their own value, and other users cannot retrieve that particular set of data, then you should use msg.sender and at the same, you should also let go of the function parameter. Your code should look like: 

function getNum() public view returns (uint) {

   return map23[msg.sender];


Give yourself time to explore the Blockchain field properly. Enroll in Blockchain Online Training now.

Browse Categories