The only reason why what @corvid suggested might not be what you’re after is - in the example you originally posted, you are checking to see how many documents the server has.
With the suggestion above, you are checking to see how many documents the client has. (you may not be subscribing/published all the documents for whatever reason)
If you want to use the Meteor.call you won’t be able to reactively update the field. You need to have a call back that sets a session variable, or a reactivevar. The helper needs to just relay that variable into your template.
I believe the best solution to this problem is the solution outlined by MDG in the documentation.
// server: publish the current size of a collection
Meteor.publish("counts-by-room", function (roomId) {
var self = this;
check(roomId, String);
var count = 0;
var initializing = true;
// observeChanges only returns after the initial `added` callbacks
// have run. Until then, we don't want to send a lot of
// `self.changed()` messages - hence tracking the
// `initializing` state.
var handle = Messages.find({roomId: roomId}).observeChanges({
added: function (id) {
count++;
if (!initializing)
self.changed("counts", roomId, {count: count});
},
removed: function (id) {
count--;
self.changed("counts", roomId, {count: count});
}
// don't care about changed
});
// Instead, we'll send one `self.added()` message right after
// observeChanges has returned, and mark the subscription as
// ready.
initializing = false;
self.added("counts", roomId, {count: count});
self.ready();
// Stop observing the cursor when client unsubs.
// Stopping a subscription automatically takes
// care of sending the client any removed messages.
self.onStop(function () {
handle.stop();
});
});
// client: declare collection to hold count object
Counts = new Mongo.Collection("counts");
// client: subscribe to the count for the current room
Tracker.autorun(function () {
Meteor.subscribe("counts-by-room", Session.get("roomId"));
});
// client: use the new collection
console.log("Current room has " +
Counts.findOne(Session.get("roomId")).count +
" messages.");
// server: sometimes publish a query, sometimes publish nothing
Meteor.publish("secretData", function () {
if (this.userId === 'superuser') {
return SecretData.find();
} else {
// Declare that no data is being published. If you leave this line
// out, Meteor will never consider the subscription ready because
// it thinks you're using the added/changed/removed interface where
// you have to explicitly call this.ready().
return [];
}
});
This way you will keep the count reactive without publishing the entire collection to the client. I also like this method because it doesn’t require you to install any additional packages to your project.
It will not be reactive. The meteor call will only run once because the helper function will only get called once on page render. The session variable is reactive and it will give you the correct output exactly once per page load. If the count changes the helper will not re-run.
if the Session were to be updated elsewhere, then the helper would run again. However since the helper runs once, and only calls the method once that means the Session only updates once. It will not reactively update with changes on the server… I was wrong!
Corvid, this solution is fine when you don’t use limit to paginate the results.
Since is good to use pagination to display information the recomendation is to use the cosio55’s soluction