state Variable is this : mapping(address=>uint) public userVotes;

now i have a function :

function votePresident(uint _id) external {
        userVotes[msg.sender] = _id;
        Voter(msg.sender, _id, userVotes[msg.sender]);

the last line is an event. So What happens here is after executing this function, I put an address of the executer in a mapping as a key and value as he passed in. Then I log it.

I have another function where I use this userVotes

function getPresident(uint _index) public view returns (uint, string, uint, string, uint) {
        President storage president = presidents[_index];
   return (president.voteCount, president.hashImage, president.id, president.name, userVotes[msg.sender] );

look at the return line the last parameter, userVotes[msg.sender] . Unfonrtunatelly, it always returns 0. I don't know why it does that. I checked the log after executing the first function and I didn't see 0. i just saw the actual value. Please help . If I call the getPresident funcction in remix, it returns the actual value and not 0.

  • Have you tried to simplify your code to isolate the parts you think are broken? Try building a minimal, complete, and verifiable example. In general, you are using msg.sender just fine in these situations from what I can see. Commented Aug 25, 2018 at 21:30
  • The question is why the same code works on remix .
    – Chemistry
    Commented Aug 25, 2018 at 21:32
  • Compared to what? I don't think you mentioned which platform this code is failing... Commented Aug 25, 2018 at 21:36
  • I'm using web3 to make the calls to contract. When I deploy the contract, and Try to test it using remix, it works on remix, but when I call the same function getPresident stated above, userVotes[msg.sender] is always 0.
    – Chemistry
    Commented Aug 25, 2018 at 21:38
  • Add a link to your contract deployment. Do you know if your transactions are successfully mined? Commented Aug 25, 2018 at 21:39

1 Answer 1


Anyone Might need it.

Sometimes you may forget to pass an account when you're actually calling a free method that's just returning something. In my case, I was calling a free (no-gas) function and because I didn't use the send transaction, I was forgetting to pass the account name. You can pass the account to the call function as you'd do for the send function.

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.