【问题标题】:How to subscribe a publish collection in Meteor?如何在 Meteor 中订阅发布集合?
【发布时间】:2012-09-12 01:32:09
【问题描述】:

我从:How does the messages-count example in Meteor docs work? 复制代码,它不起作用。客户端调用Counts.find().count()方法,我希望它输出1但结果是0,你能告诉我为什么吗?

//server code
if (Meteor.is_server)
{
    Meteor.startup(function (){
      console.log("server is startup...");
      Messages = new Meteor.Collection("messages");
      if(Messages.find().count() == 0){
      for(var i=0;i<7;i++){
        Messages.insert({room_id:"00"+i,text:"message "+i});
      }
    }
    console.log("room_id:001 messages count="+Messages.find({room_id:"001"}).count());
    //print--->room_id:001 messages count=1 (it's ok)

    Meteor.publish("counts-by-room", function (roomId) {
      var self = this;
      var uuid = Meteor.uuid();
      var count = 0;

      var handle = Messages.find({room_id: roomId}).observe({
        added: function (doc, idx) {
        count++;
        self.set("counts", uuid, {roomId: roomId, count: count});
        self.flush();
      },
      removed: function (doc, idx) {
      count--;
      self.set("counts", uuid, {roomId: roomId, count: count});
      self.flush();
    } 
    // don't care about moved or changed
    });
    // remove data and turn off observe when client unsubs
    self.onStop(function () {
      handle.stop();
      self.unset("counts", uuid, ["roomId", "count"]);
      self.flush();
     });
     });
     });
}

//client code
if (Meteor.is_client)
{
    Meteor.startup(function () {
      Counts = new Meteor.Collection("counts");
      Session.set("roomId","001");
      Meteor.autosubscribe(function () {
      Meteor.subscribe("counts-by-room", Session.get("roomId"));
    });
    console.log("I client,Current room "+Session.get("roomId")+" has " 
     + Counts.find().count() + " messages.");
    //print--->I client,Current room 001 has 0 messages.(trouble:I expect it to output "...has 1 messages" here)
    }); 
}

【问题讨论】:

    标签: meteor publish-subscribe


    【解决方案1】:

    我尝试了很多次,我发现了错误。 将客户端代码更改为如下所示,它将打印正确的结果。

    //client code
    Meteor.startup(function () {
      Counts = new Meteor.Collection("counts");
      Session.set("roomId","001");
      Meteor.autosubscribe(function () {
        Meteor.subscribe("counts-by-room", Session.get("roomId"));
        data = Counts.findOne();
        if(data){
          console.log("I client,Current room "+Session.get("roomId")+" has " 
         + data.count + " messages.");
         //print--->I client,Current room 001 has 1 messages.(now it's ok!)
        }
     })
    });
    

    【讨论】:

      【解决方案2】:

      这样试试

      Counts = new Meteor.Collection("counts-by-room"); /* correct name of the collection */
      /*... subscribing stuff */
      Counts.findOne('counts') /* this is the count you published */
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-26
        • 2016-11-29
        • 2017-11-30
        • 2013-11-18
        • 2016-03-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多