4

I was trying to transfer some ether using ganache desktop application in nodejs. It was properly working but suddenly an error occurred.

var s = 1;

try{
 web3.eth.sendTransaction({
 from: '0x627306090abaB3A6e1400e9345bC60c78a8BEf57',
 to:'0x821aea9a577a9b44299b9c15c88cf3087f3b5544',
 value: web3.toWei(s.toString(), "ether")

}).then(function(receipt) {
    console.log(receipt);
    res.json({success: true});

}).catch(function(e){
console.log('error occured');
console.log(e);
res.json({success: false });
});

If .then() and .catch() is removed, ether is transferred.

4
  • if you are not using promises. Try using fallback fuctions Feb 15, 2018 at 5:40
  • 1
    @PrashantPrabhakarSingh I think you mean "callback" functions.
    – user19510
    Feb 15, 2018 at 7:22
  • 1
    What version of web3.js are you using? 0.2x.0 doesn't support promises, but 1.0.0-beta.x does.
    – user19510
    Feb 15, 2018 at 7:22
  • I'm using 0.18.4 version and it was working perfectly..suddenly today this error showed up...
    – angelmaria
    Feb 15, 2018 at 9:42

2 Answers 2

3

You need to use a callback. Kindly read the documentation.

var handleReceipt = (error, receipt) => {
  if (error) console.error(error);
  else {
    console.log(receipt);
    res.json(receipt);
  }
}

web3.eth.sendTransaction({
 from: '0x627306090abaB3A6e1400e9345bC60c78a8BEf57',
 to:'0x821aea9a577a9b44299b9c15c88cf3087f3b5544',
 value: web3.toWei(s.toString(), "ether")
}, handleReceipt);
0
0

This is straight from the documentation:

web3.eth.sendTransaction({from: '0x123...', data: '0x432...'})
.once('sending', function(payload){ ... })
.once('sent', function(payload){ ... })
.once('transactionHash', function(hash){ ... })
.once('receipt', function(receipt){ ... })
.on('confirmation', function(confNumber, receipt, latestBlockHash){ ... })
.on('error', function(error){ ... })
.then(function(receipt){
    // will be fired once the receipt is mined
}); and it does not recognize .once, .on, or.then
or.catch
1
  • Please, reformat your answer, use code snippets so others can better understand what you were trying to say. Jul 21, 2021 at 14:04

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.