node.js - UnhandledPromiseRejectionWarning:错误 [ERR_HTTP_HEADERS_SENT]
问题描述
UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0] at ServerResponse.setHeader (_http_outgoing.js:526:11)
[0] at ServerResponse.header (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:771:10)
[0] at ServerResponse.send (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:170:12)
[0] at ServerResponse.json (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:267:15)
[0] at C:\Users\12345\Desktop\Capstone Project\src\controllers\/subjectController.js:35:43
[0] at processTicksAndRejections (internal/process/task_queues.js:97:5)
[0] (node:7684) 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: 11)
[0] (node:7684) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0] at ServerResponse.setHeader (_http_outgoing.js:526:11)
[0] at ServerResponse.header (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:771:10)
[0] at ServerResponse.send (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:170:12)
[0] at ServerResponse.json (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:267:15)
[0] at C:\Users\12345\Desktop\Capstone Project\src\controllers\/subjectController.js:35:43
[0] at processTicksAndRejections (internal/process/task_queues.js:97:5)
[0] (node:7684) 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: 12)
[0] (node:7684) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0] at ServerResponse.setHeader (_http_outgoing.js:526:11)
[0] at ServerResponse.header (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:771:10)
[0] at ServerResponse.send (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:170:12)
[0] at ServerResponse.json (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:267:15)
[0] at C:\Users\12345\Desktop\Capstone Project\src\controllers\/subjectController.js:35:43
[0] at processTicksAndRejections (internal/process/task_queues.js:97:5)
[0] (node:7684) 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: 13)
[0] (node:7684) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0] at ServerResponse.setHeader (_http_outgoing.js:526:11)
[0] at ServerResponse.header (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:771:10)
[0] at ServerResponse.send (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:170:12)
[0] at ServerResponse.json (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:267:15)
[0] at C:\Users\12345\Desktop\Capstone Project\src\controllers\/subjectController.js:35:43
[0] at processTicksAndRejections (internal/process/task_queues.js:97:5)
[0] (node:7684) 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: 14)
[0] (node:7684) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0] at ServerResponse.setHeader (_http_outgoing.js:526:11)
[0] at ServerResponse.header (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:771:10)
[0] at ServerResponse.send (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:170:12)
[0] at ServerResponse.json (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:267:15)
[0] at C:\Users\12345\Desktop\Capstone Project\src\controllers\/subjectController.js:35:43
[0] at processTicksAndRejections (internal/process/task_queues.js:97:5)
[0] (node:7684) 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: 15)
[0] (node:7684) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0] at ServerResponse.setHeader (_http_outgoing.js:526:11)
[0] at ServerResponse.header (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:771:10)
[0] at ServerResponse.send (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:170:12)
[0] at ServerResponse.json (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:267:15)
[0] at C:\Users\12345\Desktop\Capstone Project\src\controllers\/subjectController.js:35:43
[0] at processTicksAndRejections (internal/process/task_queues.js:97:5)
[0] (node:7684) 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: 16)
[0] (node:7684) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0] at ServerResponse.setHeader (_http_outgoing.js:526:11)
[0] at ServerResponse.header (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:771:10)
[0] at ServerResponse.send (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:170:12)
[0] at ServerResponse.json (C:\Users\12345\Desktop\Capstone Project\node_modules\express\lib\response.js:267:15)
[0] at C:\Users\12345\Desktop\Capstone Project\src\controllers\/subjectController.js:35:43
[0] at processTicksAndRejections (internal/process/task_queues.js:97:5)
[0] (node:7684) 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
[0] at C:\Users\12345\Desktop\Capstone Project\src\controllers\/subjectController.js:35:43
[0] at processTicksAndRejections (internal/process/task_queues.js:97:5)
[0] (node:7684) 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: 19)
router.post('/', passport.authenticate('jwt', { session: false }), (req, res) => {
for (var i = 0; i < req.body.subjects.length; i++) {
const subject = req.body.subjects[i];
const { errors, isValid } = validateSubjectInput(subject);
// Check validation
if (!isValid) {
return res.status(400).json(errors);
}
const subjectFields = {};
for (var key in subject) {
if (subject[key]) subjectFields[key] = subject[key].trim();
}
subjectFields.id = subject.id ? subject.id.trim() : '';
console.log(subjectFields);
// See if there is already a subject with the subject ID
Subject.findOne({ _id: subject._id }).then(subject => {
if (subject) {
// Subject already exists
Subject.findOneAndUpdate(
{ _id: subject._id },
{ $set: subjectFields },
{ new: true }
).then(subject => res.json(subject));
}
// Subject does not already exist
else {
new Subject(subjectFields).save().then(subject => res.json(subject));
}
});
}
});
解决方案
最好至少有一个更详细的问题,而不是复制粘贴您的错误消息。
据我所见, express 出现错误,因为您试图多次返回响应。这就是为什么您要创建一个 for 循环并在每次迭代中发送响应。
批量更新的正确方法是检查每个用户的 db 并更新或创建每个操作的结果应存储在数组中。在请求中的所有用户都被迭代之后,然后您在响应中返回结果数组。
在不过多更改代码的情况下,它可能看起来像:
router.post('/', passport.authenticate('jwt', { session: false }), (req, res) => {
const result = []
for (var i = 0; i < req.body.subjects.length; i++) {
const subject = req.body.subjects[i];
const { errors, isValid } = validateSubjectInput(subject);
// Check validation
if (!isValid) {
result.push(errors);
} else {
const subjectFields = {};
for (let key in subject) {
if (subject[key]) subjectFields[key] = subject[key].trim();
}
subjectFields.id = subject.id ? subject.id.trim() : '';
console.log(subjectFields);
// See if there is already a subject with the subject ID
// with option {new: true} it will create an user if not exists
Subject.findOneAndUpdate(
{ _id: subject._id },
{ $set: subjectFields },
{ new: true }
).then(subject => result.push(subject));
}
}
res.json(result);
});
推荐阅读
- android - 使用 registerForActivityResult 在没有权限的情况下获取联系人
- javascript - 真正的密码破解者 - Codewars 挑战 - Javascript
- react-native - 如何防止后退按钮在我的反应本机应用程序中隐藏我的键盘?
- sql - 在 SSRS 报表生成器中选择带参数的前 X 行
- github - 使用 GCP 的自动化 Github
- bazel - 构建 nuro_image 时如何确定使用哪个基础?
- google-apps-script - 突然:一天使用太多电脑时间的服务
- hash - 为什么将 f64 转换为 u64 然后再转换回 f64 会导致不同的值?
- javascript - html 中的 p5.js 不起作用(使用实例化)
- python - 当从前一个变量中减去新值时,如何中断?