How to handle mongo db unable to connect issue with Mongoose and NodeJs?

帅比萌擦擦* 提交于 2019-12-11 21:26:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!