为了账号安全,请及时绑定邮箱和手机立即绑定

如何解决节点上未处理的应答警告?

如何解决节点上未处理的应答警告?

牛魔王的故事 2022-09-23 09:21:00
我正在尝试制作一个用于学习目的的API。API 没有任何问题。我得到了我想要的回应。但问题是,当我查看终端时,我得到一个这样的错误:未经处理的应答警告:未经处理的承诺拒绝。此错误起源于在没有 catch 块的情况下抛入异步函数内部,或者拒绝未使用 .catch() 处理的承诺。它警告我,不处理权限拒绝将终止 Node.js。控制台日志 (node:8721) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client    at ServerResponse.setHeader (_http_outgoing.js:526:11)    at ServerResponse.header (/backend/node_modules/express/lib/response.js:771:10)    at ServerResponse.send (/backend/node_modules/express/lib/response.js:170:12)    at ServerResponse.json (/backend/node_modules/express/lib/response.js:267:15)    at exports.apiFormat (/backend/handlers/errorHandlers.js:85:21)    at /backend/middlewares/auth.js:31:11    at allFailed (/backend/node_modules/passport/lib/middleware/authenticate.js:107:18)    at attempt (/backend/node_modules/passport/lib/middleware/authenticate.js:180:28)    at Strategy.strategy.fail (/backend/node_modules/passport/lib/middleware/authenticate.js:302:9)    at verified (/backend/node_modules/passport-local/lib/strategy.js:82:30)    at /backend/middlewares/passport.js:69:24    at processTicksAndRejections (internal/process/task_queues.js:97:5)(node:8721) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)(node:8721) [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.我为相同的 json reson resonse 格式 api 制作它。( 错误处理程序.js)
查看完整描述

1 回答

?
qq_花开花谢_0

TA贡献1835条经验 获得超6个赞

validationResult()返回结果对象,但不返回承诺。您正在使用 等待承诺对象。await

从代码中删除以防止错误:await

const errors = validationResult(req);// .. and your error variable will have arrays of error.


查看完整回答
反对 回复 2022-09-23
  • 1 回答
  • 0 关注
  • 52 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信