4

This question already has an answer here:

I have a not constant function in my contract, with a boolean return value, when i call this function via web3, i always get 'undefined'

Contract myContract {
     function setFinished(bool _newStatus) public ifProducer returns(bool){
        done = _newStatus;
        witdrawFunds();
        return true;
     }


var result = myContract.setFinished(true,{from: web3.eth.accounts[0], gas:3000000}, function(err, res){}); 

neither result nor res get me the return value.

marked as duplicate by Ismael, lungj, Achala Dissanayake, Rob Hitchens, Richard Horrocks Sep 21 '17 at 22:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Did you shorten your code? The result should be in the callback function, so within the function you call in setFinished, you should do something like

var result = myContract.setFinished(true,{from: web3.eth.accounts[0], gas:3000000}, function(err, res){ console.log(res) });

and that should output the transaction hash of the function.

Keep in mind though, that this will return the transaction hash, and not the boolean value itself. If you want the boolean value, you need to get the function through a call, as in

var result = myContract.setFinished.call(true,{from: web3.eth.accounts[0], gas:3000000}, function(err, res){ console.log(res) });

However, calling this function through a call will not change the state (i.e., you will not change the state of done.

  • 2
    In the function call witdrawFunds() i send Funds to an other account, so this does affect the state. How can i get the return value and call this function? – TimRicta Sep 21 '17 at 13:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.