繁体   English   中英

Mean.js查询var无效

[英]Mean.js Query var not working

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

  // Comments Routes
 app.route('/api/comments/:articleId').all()
    .get(comments.listbyArticle);

在服务器cotroller上

    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

它响应所有评论,除​​了这篇文章ID,如果我硬编码( '57550c21612bc90478333017' )文章ID而不是req.articleId 。然后回复显示我正确的评论。

请解释我有什么问题?

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

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);
                }
            });
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM