Im using Mean.js to create a blog, i installed it with npm, and i create crud module comment to comment each article, and i save some comment with
article
// Comments Routes
app.route('/api/comments/:articleId').all()
.get(comments.listbyArticle);
exports.listbyArticle = function(req, res) {
Comment.find( {article : req.articleId }).sort('-created').populate('user', 'displayName').exec(function(err, comments) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(comments);
}
});
};
http://localhost:3000/api/comments/57550c21612bc90478333017
'57550c21612bc90478333017'
req.articleId
You should use req.params
to access the articleId in the URL:
exports.listbyArticle = function(req, res) {
var articleId = req.params.articleId;
Comment.find( {article: articleId }).sort('-created')
.populate('user', 'displayName')
.exec(function(err, comments) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(comments);
}
});
};