I was following a course where the instructor update a mongoose document which looks like this
.put(authenticate.verifyUser, (req, res, next) => {
User.findById(req.params.userId) //get the user
.then(
(user) => {
if (user != null && user.comments.id(req.params.commentId) != null) {
//update the comment
user.save().then(
(user) => {
User.findById(user._id)
.then((user) => {
//response logic
});
},
(err) => next(err) //(1) Is this error occur when the .save() throw any error?
);
} else if (user == null) {
//handle error
return next(err);
} else {
//handle error
return next(err);
}
},
(err) => next(err) //(2) When this error occur?
)
.catch((err) => next(err));
})
But I have a hard time understanding when which error is thrown by (err) => next(err)
. Can anyone explain it?