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

我正在尝试制作一个用于学习目的的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)



牛魔王的故事
浏览 57回答 1
1回答

qq_花开花谢_0

validationResult()返回结果对象,但不返回承诺。您正在使用 等待承诺对象。await从代码中删除以防止错误:awaitconst errors = validationResult(req);// .. and your error variable will have arrays of error.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript