【问题标题】:Modifying find() to include parameter修改 find() 以包含参数
【发布时间】:2015-08-15 04:42:14
【问题描述】:

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

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

cmets.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 并没有像我假设的那样作为字符串通过。

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

【问题讨论】:

    标签: javascript meanjs


    【解决方案1】:

    Mongoose 和 Mongodb,具有以下 find 方法签名:

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

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

    More examples

    【讨论】:

    • 对不起,我不太明白。您能否解释一下,或者编辑该功能,以便我可以看到它应该是什么。我已经阅读了您链接的内容并且无法理解,所以我在这里发布。
    猜你喜欢
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多