我正在NodeJS应用程序上运行这段代码:
let destinationBalanceWei = web3.eth.getBalance(process.env.DESTINATION_WALLET_ADDRESS).then(result => result.toString())
let destinationBalance = web3.utils.fromWei(destinationBalanceWei, 'ether')但是我在控制台中得到了以下错误:
(node:79279) UnhandledPromiseRejectionWarning: Error: Please pass numbers as strings or BigNumber objects to avoid precision errors.
at Object.fromWei (/Users/sahelanthropus/progetti/nodejs/node_modules/web3-utils/dist/web3-utils.cjs.js:440:11)
at proveTransaction (/Users/sahelanthropus/progetti/nodejs/routes/index.js:49:39)
at /Users/sahelanthropus/progetti/nodejs/routes/index.js:105:7
at IncomingForm.<anonymous> (/Users/sahelanthropus/progetti/nodejs/node_modules/formidable/lib/incoming_form.js:107:9)
at IncomingForm.emit (events.js:197:13)
at IncomingForm._maybeEnd (/Users/sahelanthropus/progetti/nodejs/node_modules/formidable/lib/incoming_form.js:557:8)
at /Users/sahelanthropus/progetti/nodejs/node_modules/formidable/lib/incoming_form.js:238:12
at WriteStream.<anonymous> (/Users/sahelanthropus/progetti/nodejs/node_modules/formidable/lib/file.js:79:5)
at Object.onceWrapper (events.js:285:13)
at WriteStream.emit (events.js:197:13)
at finishMaybe (_stream_writable.js:646:14)
at stream._final (_stream_writable.js:624:5)
at WriteStream._final (internal/fs/streams.js:270:3)
at callFinal (_stream_writable.js:617:10)
at processTicksAndRejections (internal/process/next_tick.js:76:17)
(node:79279) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:79279) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.我能做些什么来解决这个问题?
发布于 2019-02-14 09:57:53
这不正确地处理异步函数调用:
let destinationBalanceWei = web3.eth.getBalance(...).then(result => result.toString());
let destinationBalance = web3.utils.fromWei(destinationBalanceWei, 'ether');当您将destinationBalanceWei传递给fromWei时,它的值可能是undefined。
你可以这样做:
let destinationBalance;
web3.eth.getBalance(...).then(function(destinationBalanceWei) {
console.log(destinationBalanceWei.toFixed());
destinationBalance = web3.utils.fromWei(destinationBalanceWei, 'ether');
});您必须找到一种方法来确保已经设置了destinationBalance。
或者你可以这样做:
async function func() {
let destinationBalanceWei = await web3.eth.getBalance(...);
console.log(destinationBalanceWei.toFixed());
let destinationBalance = web3.utils.fromWei(destinationBalanceWei, 'ether');
return destinationBalance;
}我个人认为后者更方便,除非您当然有一个很好的理由,您必须在不等待它完成的情况下运行getBalance (与性能相关的原因)。
发布于 2019-02-14 09:44:56
问题是web3.eth.getBalance返回一个承诺,您必须“等待”直到它被执行。
您可以使用ES2018并将代码封装在异步函数中:
async function myFunc() {
let destinationBalanceWei = await web3.eth.getBalance(process.env.DESTINATION_WALLET_ADDRESS);
let destinationBalance = web3.utils.fromWei(destinationBalanceWei, 'ether');
console.log("destinationBalance", destinationBalance);
}另外,请注意,web3.utils.fromWei方法需要作为第一个参数传递的字符串或BigNumber对象。destinationBalanceWei已经是一个字符串了,所以在上面的场景中我们不必考虑这一点。
https://ethereum.stackexchange.com/questions/67076
复制相似问题