5

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.

0

1 Answer 1

3

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.

1
  • 4
    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
    Commented Sep 21, 2017 at 13:57

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