我正在嘗試制作一個用于學習目的的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)
如何解決節點上未處理的應答警告?
牛魔王的故事
2022-09-23 09:21:00