Reputation: 451
when i send a request to this function it show me this error :
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 :
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 ?
Upvotes: 0
Views: 27
Reputation: 7665
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]
. 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]);
}
Upvotes: 1