EasonBlack
EasonBlack

Reputation: 4426

How could I know if the mongodb is started or not by mongoose?

I have a working site with node.js + Express + mongoose.

I am afraid there will be chance that the MongoDB will be shut down by accident or maybe it wasn't started at first.

The following is the code:

var  mongoose    = require('mongoose'),
     Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/Test');

var Student = mongoose.model('student', new Schema());

Student.find({...},function(err,docs){
      do sth
});

As I see , there will be no err message when find in no Mongo situation. It was just blocked.

And I didn't find a property in mongoose to show the connection status.

So anyone know how could I know the status of the mongodb in NodeJs?

Upvotes: 1

Views: 1114

Answers (1)

Stennie
Stennie

Reputation: 65333

The err parameter is a standard Error object which will be set if there any exceptions such as the database connection being unavailable. You do not need to check the connection status .. you need to check err and handle appropriately.

It would be worth having a read of the introduction to MongoDB's node driver for some example usage.

See also Error handling for Mongoose.

Upvotes: 2

Related Questions