Mean.js 查询变量不工作

Mean.js Query var not working

我使用 Mean.js 创建了一个博客,我用 npm 安装了它,我创建了 crud 模块评论来评论每篇文章,我用 article id 保存了一些评论作为参考。我在服务器中创建 api 路由。

  // 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') 文章 ID 而不是 req.articleId,它会响应除这篇文章 ID 之外的所有评论。然后响应会显示正确的评论。

请解释一下哪里出了问题?

您应该使用 req.params 访问 URL:

中的文章 ID
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);
                }
            });
};