修改 find() 以包含参数

Modifying find() to include parameter

我为每篇文章创建了一个评论模块,我想通过查找函数发送 Article._id 参数,因此它只是 returns 与该线程相关的评论。评论模块有一个文章 ID 作为其集合的一部分,作为识别关系的一种方式。

我熟悉 Java 但 Mean.js 对我来说是新手,我无法弄清楚为什么变量不会通过。

comments.server.controller.js

exports.list = function(req, res, id) {
    Comment.find()
        .sort('-created')
        .where('articleID', id)
        .populate('userName', 'details','created','user')
        .exec(function(err, comments) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.jsonp(comments);
        }
    });
};

view.article.client.view.js

 <section data-ng-controller="CommentsController" data-ng-init="find($scope.deal._id)">

我想这就是您需要从视图中看到的全部内容。

如果我删除 where 子句,它 returns 它们都很好,但显然 $scope.deal._id 并没有像我假设的那样作为一个字符串。

如何正确地将字符串发送到函数?

Mongoose,因此 Mongodb,具有以下查找方法签名:

Model.find(查询、字段、选项、回调)

因此您可以在查询参数中放置对象以便与集合相匹配。例如 Comment.find({articleID: id_variable})

More examples