How to wait on the client side until connection to mongo on the server side is ready? How to wait on the client side until connection to mongo on the server side is ready? mongodb mongodb

How to wait on the client side until connection to mongo on the server side is ready?


One primitive way of doing this is listening for a change in Meteor.userId() using Meteor.autorun. If you are able to get this, you would know that you have connected to MongoDB. If you are not dealing with authentication, then you could create a method on the server side which returns something from MongoDB. When it returns something, on success in the client side you could start all of the subscriptions.


The most reliable way of doing this is via a Meteor.call invocation. If you do this as a synchronous call (no callback), the client will wait until the call completes. Here's how to do it asynchronously:

  Meteor.call('isEverythingReady', param1,    function(error, result) {      if (error === undefined) {        Meteor.subscribe("mystuff");        Session.set("sess1", "whatever");      } else {        alert("There was an error during startup.");      }    });

and then

if (Meteor.isServer) {  Meteor.methods( {    isEverythingReady: function(param1) {      // can you connect to database?      return true;    }  }}