简体   繁体   English

Mean.js查询var无效

[英]Mean.js Query var not working

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 id as reference. 我使用Mean.js创建一个博客,我用npm安装它,我创建crud模块评论来评论每篇文章,我保存一些评论与article ID作为参考。 and i create api route in server. 我在服务器中创建api路由。

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

on server cotroller 在服务器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);
       }
     });
   };

but when navigate to this route 但是当导航到这条路线时

http://localhost:3000/api/comments/57550c21612bc90478333017

it response all comments except this article id, if i hard code ( '57550c21612bc90478333017' ) the article id instead of req.articleId .Then response shows me proper comments. 它响应所有评论,除​​了这篇文章ID,如果我硬编码( '57550c21612bc90478333017' )文章ID而不是req.articleId 。然后回复显示我正确的评论。

please explain me what is wrong? 请解释我有什么问题?

You should use req.params to access the articleId in the URL: 您应该使用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