Have problem in Collection.find().count()

This is my code:

Template.Users.helpers({
users(){
console.log("Users : ", Users.find().count());
return Users.find();
},
});

{{#each users}} {{shortName}} {{name}} {{code}} {{balance}} {{/each}}

I am using DATATABLE to display the data! Once No data available in table and all data displayed successfully and other time No data available in table doesn’t diplayed to me and the data displayed successfully !
So that makes me a little crazy !
The display when I use console.log("Users : ", Users.find().count()); give me Users : 0 and Users : 9 (the real number of documents) in the same time !
Any help ?

It is 0 when the subscription is not finished. When all the data arrives at the client, the count is updated to 9. You can read more about it here.

1 Like

How can I accelerate the subscription to display my data in loading page ?

The time that is needed for all data to be loaded depends on your server, your publication, … Try to optimize your publications first.
You can use a neat template helper Template.subscriptionsReady. You should read this. The blog post explains how you can subscribe to the data and wait for it to be loaded.

1 Like

You basically have 2 options for this. Server Side Rendering, or the fast-render package which will make subscription data available on the client when the page first renders by sending the data down with the initial html of the page.

1 Like

Sometimes you gotta deal with latency - network, db, etc.

As of Meteor 1.0.4, there is a helper that tells you exactly when a particular subscription is ready: Template.instance().subscriptionsReady().

I usually show a spinner or something while this returns false then show the template loaded with data once it’s true.

You can animate the show with fade in etc if you want.

subscriptionsReady is reactive so you can check it in a blaze helper to drive whatever behavior you want.

1 Like