I'm learning MEAN stack with an awesome tutorial online. In it, there is a section about preloading objects by id using express's param().
router.param('post', function(req, res, next, id) {
var query = Post.findById(id);
query.exec(function (err, post){
if (err) { return next(err); }
if (!post) { return next(new Error('can\'t find post')); }
req.post = post;
return next();
});
});
My question are:
Where can I find the API documentation on the exec() function? Is it a Mongoose function? How can I tell if it's a Mongoose function or JS or express function?
What's the purpose of final
return next()
here? Is it necessary in the exec() function to havereturn next()
? What would happen if that line is absent? I have read that next() is for the next middleware, but in other functions from the tutorial, like below, there is no finalnext()
orreturn next()
. What's the difference betweennext()
andreturn next()
anyways?router.post('/posts', function(req, res, next) { var post = new Post(req.body); post.save(function(err, post){ if(err){ return next(err); } res.json(post); }); });
Thanks.