我正在嘗試制作一個(gè)用于學(xué)習(xí)目的的API。API 沒有任何問題。我得到了我想要的回應(yīng)。但問題是,當(dāng)我查看終端時(shí),我得到一個(gè)這樣的錯(cuò)誤:未經(jīng)處理的應(yīng)答警告:未經(jīng)處理的承諾拒絕。此錯(cuò)誤起源于在沒有 catch 塊的情況下拋入異步函數(shù)內(nèi)部,或者拒絕未使用 .catch() 處理的承諾。它警告我,不處理權(quán)限拒絕將終止 Node.js??刂婆_(tái)日志 (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)我為相同的 json reson resonse 格式 api 制作它。( 錯(cuò)誤處理程序.js)exports.apiFormat = (statusCode, responseCode, message, success, data, res) => { try { return res.status(statusCode).json({ message: message, success: Boolean(success), data: data, status: Number(responseCode), }); } catch (error) { res.status(500).json({ message: "Internal Server Error", }); }};
如何解決節(jié)點(diǎn)上未處理的應(yīng)答警告?
肥皂起泡泡
2022-09-23 09:21:35