Help regarding Meteor Pub/Sub

120 views Asked by At

I have more than one million records in mongo collection. It is obviously is bad strategy to send all these record to client browser.

So, I created a publish function which just return count for "Searched" parameter only amongst those million records.

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

So, I got response from above query in following format.

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

I want to show the count from above publication,But I receive objects only in response not number. Though When I executed this same query in dev console then , it did showed "Number" as output.

Where am I going wrong? Can anyone show me a simple example that helps this to resolve?

1

There are 1 answers

0
Tomas Hromnik On

There is a package for it: https://atmospherejs.com/tmeasday/publish-counts

Simply call Counts.publish within a publication, passing in a name and a cursor:

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

Subscribe on the client:

Meteor.subscribe('posts');

Then just call Counts.get('postsCount'); to get counter number. For example in your helper:

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