首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何查找Node.js UnhandledPromiseRejectionWarning中未处理的承诺?

如何查找Node.js UnhandledPromiseRejectionWarning中未处理的承诺?
EN

Stack Overflow用户
提问于 2017-05-08 01:31:41
回答 2查看 77K关注 0票数 206

来自版本7的Node.js具有异步/等待语法糖来处理承诺,现在在我的代码中,以下警告经常出现:

代码语言:javascript
复制
(node:11057) UnhandledPromiseRejectionWarning: Unhandled promise 
rejection (rejection id: 1): ReferenceError: Error: Can't set headers 
after they are sent.
(node:11057) 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.

不幸的是,这里没有引用到缺少catch的那一行。有没有办法不检查每个try/catch块就能找到它?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-16 15:27:00

监听进程的unhandledRejection事件。

代码语言:javascript
复制
process.on('unhandledRejection', (reason, p) => {
  console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
  // application specific logging, throwing an error, or other logic here
});
票数 339
EN

Stack Overflow用户

发布于 2019-03-16 09:35:46

使用堆栈跟踪进行日志记录

如果您正在寻找更多有用的错误消息。尝试将其添加到您的节点文件中。它应该显示发生崩溃的完整堆栈跟踪。

代码语言:javascript
复制
process.on('unhandledRejection', (error, p) => {
  console.log('=== UNHANDLED REJECTION ===');
  console.dir(error.stack);
});
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43834559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档