[英]Cannot set headers after they are sent to the client when return the response in nodejs
when i send a request to this function it show me this error:当我向此 function 发送请求时,它显示此错误:
events.js:292
throw er; // Unhandled 'error' event
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:533:11)
at ServerResponse.header (F:\Projects\Nodejs\SalesSignal\node_modules\express\lib\response.js:771:10)
at ServerResponse.send (F:\Projects\Nodejs\SalesSignal\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (F:\Projects\Nodejs\SalesSignal\node_modules\express\lib\response.js:267:15)
at ServerResponse.send (F:\Projects\Nodejs\SalesSignal\node_modules\express\lib\response.js:158:21)
at ManagerController.Ok (F:\Projects\Nodejs\SalesSignal\src\http\controller\BaseController.js:18:28)
at F:\Projects\Nodejs\SalesSignal\src\http\controller\ManagerController.js:121:25
my function is this and this function fir update record:我的 function 是这个和这个 function 杉木更新记录:
async DeleteManager(req, res, next) {
let result = await this.ValidationAction(req, res);
if (result[0]) {
const user = Manager.findByIdAndUpdate(
req.params.id,
{ $set: { isDelete: true } },
{ useFindAndModify: false },
(error, user) => {
if (error) {
next(error);
} else if (!user) {
return res.status(200).send({
message: "رکورد مورد نظر یافت نشد",
statusCode: 200,
success: false,
});
} else {
return res.status(200).send({
message: "عملیات با موفقیت انجام شد",
statusCode: 200,
success: true,
});
}
}
);
}
return this.BadRerquest(res, result[1]);
}
when i send a request to this functin it update record in database but it show me this error.当我向这个函数发送请求时,它会更新数据库中的记录,但它显示了这个错误。
whats the problem?有什么问题? how can i solve thus problem?我该如何解决这个问题?
Most likely your app tries to send multiple responses to the single request.您的应用很可能会尝试对单个请求发送多个响应。 This may happen because the app seems to always send BadRequest
even if there is result[0]
.这可能是因为应用程序似乎总是发送BadRequest
,即使有result[0]
。 Try to change the control flow from:尝试从以下位置更改控制流:
if (result[0]) {
// ...
}
return this.BadRerquest(res, result[1]);
to至
if (result[0]) {
// ...
} else {
return this.BadRerquest(res, result[1]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.