问题
Our Nodejs application uses Mongoose for MongoDB. Our application crashes when it unable to connect to MongoDB database. We are using MongoLab
.
What are the best ways to handle database connect issue in Node JS application?
回答1:
Just handle error with the associated events:
// If the connection throws an error
connection.on('error',function (err) {
// Do something here
});
// When the connection is disconnected
connection.on('disconnected', function () {
// Do something else here
});
And, of course, when running a query, it's very recommended to always check err
parameter of the callback function before going any further.
来源:https://stackoverflow.com/questions/19265810/how-to-handle-mongo-db-unable-to-connect-issue-with-mongoose-and-nodejs