有关Meteor Pub / Sub的帮助

时间:2015-06-10 22:39:44

标签: meteor mongodb-query meteor-helper

我在mongo集合中有超过一百万条记录。 将所有这些记录发送到客户端浏览器显然是不好的策略。

所以,我创建了一个发布函数,它只返回那些百万条记录中的“搜索”参数的计数。

Meteor.publish('count', function(query) { return Collection.find({ fieldName : "query" }).count(); });

所以,我通过以下格式得到了上述查询的回复。

We are counting for pain and count is [object Object]

我想从上面的出版物中显示计数,但我收到的对象只是回复而不是数字。虽然当我在开发控制台中执行相同的查询时,它确实显示“数字”作为输出。

我哪里错了? 有人能告诉我一个简单的例子来帮助解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

有一个包:https://atmospherejs.com/tmeasday/publish-counts

只需在出版物中调用Counts.publish,传入名称和光标:

//server code
Meteor.publish('posts', function() {
  Counts.publish(this, 'postsCount', Posts.find());
  return Posts.find({}, { limit: 10 });
});

订阅客户:

Meteor.subscribe('posts');

然后只需拨打Counts.get('postsCount');即可获得计数器号码。例如在您的助手中:

Template.posts.helpers({
  postsCount: function() {
    return Counts.get('postsCount');
  }
});
相关问题