i want find record and update the the record in mongoose and nodejs .
async DeleteRole(req, res, next) {
let validationData = await this.ValidationAction(req, res);
if (validationData[0]) {
Role.findByIdAndUpdate(
req.params.id,
{ $set: { isDelete: true } },
{ useFindAndModify: false },
(error, role)=> {
console.log(role)
if (error) next(error);
if (!role) return res.status(200).send({
message: "NotFoundRecord",
statusCode: 200,
success: false,
});
}
);
return res.status(200).send({
message: "Success",
statusCode: 200,
success: true,
});
}
return res.status(200).send({
message: "BadREquest",
statusCode: 400,
success: false,
});
}
i want when record not found it show me the errro the record not found and somve one else .
but when i send a request it show me this error :
F:\Projects\Nodejs\SalesSignal\node_modules\mongoose\lib\helpers\promiseOrCallback.js:19 throw error; ^
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 RoleController.Notfound (F:\Projects\Nodejs\SalesSignal\src\http\controller\BaseController.js:26:28)
at F:\Projects\Nodejs\SalesSignal\src\http\controller\RoleController.js:25:34
at F:\Projects\Nodejs\SalesSignal\node_modules\mongoose\lib\model.js:4882:16
at F:\Projects\Nodejs\SalesSignal\node_modules\mongoose\lib\model.js:4882:16
at F:\Projects\Nodejs\SalesSignal\node_modules\mongoose\lib\model.js:4882:16
at F:\Projects\Nodejs\SalesSignal\node_modules\mongoose\lib\helpers\promiseOrCallback.js:16:11
at F:\Projects\Nodejs\SalesSignal\node_modules\mongoose\lib\model.js:4905:21
at F:\Projects\Nodejs\SalesSignal\node_modules\mongoose\lib\query.js:4382:18
at model.Query.Query._findAndModify (F:\Projects\Nodejs\SalesSignal\node_modules\mongoose\lib\query.js:3462:12)
at model.Query. (F:\Projects\Nodejs\SalesSignal\node_modules\mongoose\lib\query.js:3037:8)
at model.Query._wrappedThunk [as _findOneAndUpdate] (F:\Projects\Nodejs\SalesSignal\node_modules\mongoose\lib\helpers\query\wrapThunk.js:16:8)
Emitted 'error' event on Function instance at:
**now i want tow things : A => find the best way for find and update record and solve this problem ? how can i solve this problem ??? **
Role.findByIdAndUpdate
return a promise so that you could useawait
there as well? - Bergi