我仍然试图了解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”。
我做错了什么?
答案 0 :(得分:3)
Subsribing是异步的,您需要在尝试访问集合中的数据之前通过传递回调函数来等待订阅完成。 Javascript示例:
Meteor.subscribe('items', function () {
console.log(Items.find().count());
});