猫鼬检测数据库未准备好 [英] Mongoose detect database not ready

查看:70
本文介绍了猫鼬检测数据库未准备好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以检测到数据库未在Mongoose上运行?

Is it possible to detect that the database is not running with Mongoose ?

推荐答案

我建议使用openerror事件来检查是否可以连接到数据库.这是我所有项目中使用的一个简单示例,用于仔细检查我是否已连接.

I would recommend using the open and error events to check if you can connect to the database. This is a simple example used in all my projects to double check that I'm connected.

var mongoose = require('mongoose');

mongoose.connection.on('open', function (ref) {
  console.log('Connected to mongo server.');
});
mongoose.connection.on('error', function (err) {
  console.log('Could not connect to mongo server!');
  console.log(err);
});

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

这篇关于猫鼬检测数据库未准备好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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