【问题标题】:I cannot consume my publications in Meteor template's helper我无法在 Meteor 模板的助手中使用我的出版物
【发布时间】:2016-02-13 00:04:29
【问题描述】:

我在服务器端定义了如下发布:

  Meteor.publish('observedQuestionsFeed',function(){
        _self = this;
        return Questions.find({
            observedByUsers : {$exists: true,$elemMatch: {$eq:_self.userId}}});
    });

返回我需要的;

然后我在我的模板中定义了对该出版物的订阅,如下所示:

Template.observedQuestions.onCreated(function(){
    var self = this;
    self.autorun(function(){
        self.subscribe('observedQuestionsFeed');
    });
});

但我无法在我的助手中使用该出版物,因为我需要重复我理解的相同查询,但 $eq 无法识别;

我说:

Template.observedQuestions.helpers({
    observedQuestions : function(){
        questions =  Questions.find({
            observedByUsers : {$exists: true,$elemMatch: {$eq:Meteor.userId()}}});
        return questions;
    }
});

由于 $eq 无法识别而无法正常工作。 我只想在这个特定的模板中使用这个非常具体的出版物。我应该怎么做? (observedByUsers 字段是一个简单的 usersIds 数组,或者在我的 mongo 集合中未定义)

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    在您的发布功能中,您已经将记录过滤到您想要的内容。例如,如果这是 5 条记录,那么您只是将这 5 条记录发送给客户端。因此,您应该能够做到:

    Template.observedQuestions.helpers({
        observedQuestions : function(){
            return Questions.find();
        }
    });
    

    【讨论】:

    • 这就是我的想法,但它不仅返回了我在服务器端选择的文档的整个集合;也许它与我在页面上也使用相同集合的其他模板有关..
    猜你喜欢
    • 2014-04-04
    • 2015-01-26
    • 1970-01-01
    • 2013-01-04
    • 2016-04-22
    • 2014-06-20
    • 2015-01-06
    相关资源
    最近更新 更多