Meteor: How can I tell when the database is ready?
Solution 1:
You should first publish
the data from the server.
if(Meteor.isServer) {
Meteor.publish('default_db_data', function(){
return Games.find({});
});
}
On the client, perform the collection queries only after the data have been loaded from the server. This can be done by using a reactive session inside the subscribe
calls.
if (Meteor.isClient) {
Meteor.startup(function() {
Session.set('data_loaded', false);
});
Meteor.subscribe('default_db_data', function(){
//Set the reactive session as true to indicate that the data have been loaded
Session.set('data_loaded', true);
});
}
Now when you perform collection queries, you can check if the data is loaded or not as:
if(Session.get('data_loaded')){
Games.find({});
}
Note: Remove autopublish
package, it publishes all your data by default to the client and is poor practice.
To remove it, execute $ meteor remove autopublish
on every project from the root project directory.
Solution 2:
Use DDP._allSubscriptionsReady()
(Meteor 0.7)
Solution 3:
As of Meteor 1.0.4, there is a helper that tells you exactly when a particular subscription is ready: Template.instance().subscriptionsReady()
.
Since this question is a duplicate, please check my answer in the original question, Displaying loader while meteor collection loads.