修改find()以包含参数

Modifying find() to include parameter

本文关键字:包含 参数 find 修改      更新时间:2023-09-26

我为每篇文章创建了一个评论模块,我想发送文章。_id参数通过find函数返回与该线程相关的注释。comments模块有一个articleID作为其集合的一部分,作为识别关系的一种方式。

我熟悉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子句,它返回它们都很好,但显然$scope.deal._id不会像我假设的那样作为字符串传递。

我如何正确地通过字符串发送到函数?

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

模型。查找(查询、字段、选项、回调)

因此,在查询参数中,您可以按照与集合匹配的顺序放置对象。例如Comment。找到({正如:id_variable})

更多示例