This is what my code looks like:
userUpdate(req: Request, res: Response) {
this.userTaskObj.userUpdate(req.params.id, req.body).then(() => {
res.status(200).json({
status: 'OK',
message: 'User updated',
});
})
.catch((err) => ErrorHandler.getErrorMessage('User', res, err));
}
(below function is inside UserTask class)
userUpdate(id: string, data: any) {
let query: Promise<any>;
if (data.provides) {
query = User.findByIdAndUpdate(id, {$addToSet: {provides: data.provides}}).exec();
} else {
query = User.findByIdAndUpdate(id, data).exec();
}
return new Promise((resolve, reject) => {
resolve(query);
// query.then(res => resolve(res))
// .catch(err => reject(err));
});
}
I thought I wouldn't able to catch errors from the query using resolve alone. But it does catch mongoose errors. When I replaced it with the commented part, that too works but the errors are caught inside the catch() which is rejected. How is the code catching errors without the catch() ?