Go-Ethereum

TypeError: web3.eth.sendTransaction(…).then 不是函式

  • July 21, 2021

我試圖在nodejs中使用 ganache 桌面應用程序傳輸一些乙太。它工作正常,但突然發生錯誤。

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 });
});

如果.then( **).catch()**被刪除,則乙太幣被轉移。

您需要使用回調。請閱讀文件

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);

這直接來自文件:

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

引用自:https://ethereum.stackexchange.com/questions/39709