s中出现UnhandledPromiseRejectionWar

s中出现UnhandledPromiseRejectionWar

本文介绍了如何确定是什么原因导致Node.js中出现UnhandledPromiseRejectionWarning?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经围绕async/await库构建了Node.js应用程序,并且它在大多数情况下都运行良好.我唯一遇到的麻烦是,每当未兑现承诺时,我都会得到以下错误的一些变化:

I've structured my Node.js application around the async/await library and it has been working great most of the time. The only trouble I have with it is that whenever a promise isn't fulfilled I get some variation of the following error:

(node:83333) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Cannot read property '_id' of null

我通常能够找到有问题的承诺,但有时需要进行大量调试.有没有一种方法可以用来检查未处理的Promise的行号?将为我节省很多头痛.

I'm ussually able to locate the offending promise but it takes quite a bit of debugging sometimes. Is there a method I can use to check the line number of the unhandled promise? Would save me considerable headach.

推荐答案

我建议您设置一个全局 unhandledRejection处理程序位于条目文件的开头:

I suggest you set a global unhandledRejection handler at the very beginning of your entry file:

process.on('unhandledRejection', (reason, p) => { throw reason });

这样,即使您忘记在本地捕获错误,仍然可以轻松地找到它们.

This way, even if you forget to catch the errors locally, you can still track them down easily.

更新

上述处理程序如何帮助您似乎有些困惑.基本上,当您未捕获承诺错误时,节点会将警告输出到控制台.出于任何愚蠢的原因,节点仅输出错误消息而没有堆栈.设置处理程序,然后重新引发错误,将生成堆栈,并使您可以更轻松地调试代码.这是一个示例:

There seems to be some confusion as to how the above handler helps you. Basically, when you don't catch promise errors, node outputs that warning to the console. For whatever silly reason, node only outputs the error message without the stack. Setting up the handler and then rethrowing the error generates the stack and allows you to debug your code easier. Here's an example:

let test = () => new Promise((resolve, reject) => {
    throw new Error('Random Error'); // same as "reject(new Error('Random Error'));"
});

test();

没有处理程序,您将得到:

Without the handler you get:

(node:20012) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Random Error

然后,将处理程序添加到文件顶部:

Then, we add the handler at the top of our file:

process.on('unhandledRejection', (reason, p) => { throw reason });

let test = () => new Promise((resolve, reject) => {
    throw new Error('Random Error'); // same as "reject(new Error('Random Error'));"
});

test();

现在,我们得到了一个更好的错误堆栈:

Now we get a much nicer error stack:

(function (exports, require, module, __filename, __dirname) { process.on('unhandledRejection', (reason, p) => { throw reason });
                                                                                                                ^

Error: Random Error
    at Promise (S:\amir\test.js:5:9)
    at test (S:\amir\test.js:3:18)
    at Object.<anonymous> (S:\amir\test.js:8:1)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)

这篇关于如何确定是什么原因导致Node.js中出现UnhandledPromiseRejectionWarning?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 15:32