【问题标题】:Meteor - client can't get the collectionMeteor - 客户无法获得收藏
【发布时间】:2012-10-17 12:36:36
【问题描述】:

我仍在努力了解 Meteor 的整个发布/订阅方面。

这是我想要实现的要点。

在服务器端,在“Meteor.startup”,我从博客中获取 RSS 提要。这部分有效。基本上,我的服务器代码看起来像

Items = new Meteor.Collection "items"

Meteor.startup ->
  ..
  .. # code for fetching the RSS feeds
  ..
  for each feed
    Items.insert
      title:item.title

  console.log Items.find().count() # this returns the correct count

  Meteor.publish "items", ->
    Items.find()

现在我已经发布了“项目”,我想从客户端订阅它。

Items = new Meteor.Collection "items"
Meteor.subscribe("items")
console.log Items.find().count()

但上面给了我“0”。

我做错了什么?

【问题讨论】:

    标签: javascript coffeescript meteor


    【解决方案1】:

    订阅是异步的,您需要通过回调函数等待订阅完成,然后再尝试访问集合中的数据。 Javascript 示例:

    Meteor.subscribe('items', function () {
        console.log(Items.find().count());
    });
    

    【讨论】:

    • 我发现这不是被动的。如果发生任何数据更改,我还能在哪里计算收集项目和反应。
    猜你喜欢
    • 2016-08-13
    • 1970-01-01
    • 2015-10-15
    • 2017-02-03
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    相关资源
    最近更新 更多