Node.js : How to check if mongojs connected successfully?

2.9k views Asked by At

Im making a server and need some way to be identified if mongojs connected successfully to the mongodb database when i call the function :

    mongojs.connect(connectionString, collections);

Is there a callback or an event listener i could use?

1

There are 1 answers

5
jesusbotella On BEST ANSWER

As you can read in MongoJS docs (https://github.com/mafintosh/mongojs#events), there is one event which you can listen to know when the db is ready:

db.on('ready',function() {
    console.log('database connected');
});

You need to make a variable assignment of the sentence you posted, as I show you below:

var db = mongojs.connect(connectionString, collections);

and then use the ready event to listen the db connection