Meteor - 发布和订阅问题

时间:2016-03-05 04:22:59

标签: meteor publish-subscribe

我是Meteor的新手并试图发布并订阅工作。这是我的订阅代码:

//isClient
    Session.set("userSetLimit",10);

  Template.MoodList.helpers({
    hlpinvoices: function(){
        var curinvoices = Meteor.subscribe('invoices', Meteor.userId(),Session.get("userSetLimit"));
        return curinvoices;
    }
  });

发布:

//isServer
    Meteor.publish('invoices',function (creator,limit) {
        return Invoices.find({CreatedBy:creator},{sort:{DateCreated:-1}, limit:limit});
    })

和模板:

  <template name="MoodList">
    <ul>
      {{#each hlpinvoices}}
        {{> invoice}}
      {{/each}}
  </ul>
</template>

这就是我得到的错误:

  

延迟回调中的异常:错误:{{#each}}目前只接受   数组,游标或虚假值。

但如果我使用do:

//isClient
Session.set("userSetLimit",10);
  Template.MoodList.helpers({
    hlpinvoices: function(){
        return Invoices.find({CreatedBy:Meteor.userId()},{sort:{DateCreated:-1}, limit:Session.get("userSetLimit")});
    }
  });

我没有问题。知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

Meteor.subscribe仅为您提供订阅ID。请尝试此

    Template.MoodList.helpers({
      hlpinvoices: function(){
        Meteor.subscribe('invoices', Meteor.userId(),Session.get("userSetLimit"));
        var curunnvoices=Invoices.find({CreatedBy:Meteor.userId()},{sort:{DateCreated:-1}, limit:Session.get("userSetLimit")}) 
       return curinvoices;
      }
   });

答案 1 :(得分:0)

subscribe是为了从服务器获取数据而调用的,它实际上并不返回任何数据 - 因此错误。您需要在模板的onCreated方法中调用subscribe:

Template.MoodList.onCreated( function() {
  Meteor.subscribe('invoices', Meteor.userId(),Session.get("userSetLimit"));
});

然后在你的助手中你可以退回发票:

Template.MoodList.helpers({
    hlpinvoices: function(){
        return Invoices.find({CreatedBy:Meteor.userId()},{sort:{DateCreated:-1}, limit:Session.get("userSetLimit")});
    }
});

您可以在Meteor Guide中了解有关订阅数据的详情。希望有所帮助!