如何处理 mongo db 无法连接 Mongoose 和 NodeJs 的问题? [英] How to handle mongo db unable to connect issue with Mongoose and NodeJs?

查看:62
本文介绍了如何处理 mongo db 无法连接 Mongoose 和 NodeJs 的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的 Nodejs 应用程序使用 Mongoose for MongoDB.我们的应用程序在无法连接到 MongoDB 数据库时崩溃.我们正在使用 MongoLab.

Our Nodejs application uses Mongoose for MongoDB. Our application crashes when it unable to connect to MongoDB database. We are using MongoLab.

在 Node JS 应用程序中处理数据库连接问题的最佳方法是什么?

What are the best ways to handle database connect issue in Node JS application?

推荐答案

只处理相关事件的错误:

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
});

当然,在运行查询时,非常建议在继续之前始终检查回调函数的 err 参数.

And, of course, when running a query, it's very recommended to always check err parameter of the callback function before going any further.

这篇关于如何处理 mongo db 无法连接 Mongoose 和 NodeJs 的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆