我有一个 MEAN 项目,这是我的 server.js 的一个片段
var db = require('./config/db');
// url : 'mongodb://localhost/cdnserver'
// results are same for 127.0.0.1 or the machines ip
console.log(mongoose.connect(db.url));
mongoose.set('debug', true);
mongoose.connection.on('connected', function () {
console.log('Mongoose default connection open to ' + db.url);
});
// If the connection throws an error
mongoose.connection.on('error',function (err) {
console.log('Mongoose default connection error: ' + err);
});
// When the connection is disconnected
mongoose.connection.on('disconnected', function () {
console.log('Mongoose default connection disconnected');
});
这个设置已经运行了 3 个多月了。现在我正在将我的整个 MEAN 堆栈连同数据库复制到其他机器。我采用了 mongodump 并执行了 mongorestore。从终端通过 mongo 恢复的数据库设置看起来很好。
但是,在启动服务器时,不会调用连接打开回调。如果我停止 mongodb 服务,则会调用断开连接和错误回调。我该如何进一步调试?
我附上了两个设置的控制台输出。
设置 1:
Mongoose {
connections:
[ NativeConnection {
base: [Circular],
collections: {},
models: {},
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'cdnserver',
options: [Object],
otherDbs: [],
_readyState: 2,
_closeCalled: false,
_hasOpened: false,
_listening: false,
db: [Object] } ],
plugins: [],
models: {},
modelSchemas: {},
options: { pluralization: true } }
Server up on 80
Mongoose default connection open to mongodb://localhost/cdnserver
1
Mongoose: videos.find({}) { skip: 0, limit: 5, fields: undefined }
设置 2:
Mongoose {
connections:
[ NativeConnection {
base: [Circular],
collections: {},
models: {},
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'cdnserver',
options: [Object],
otherDbs: [],
_readyState: 2,
_closeCalled: false,
_hasOpened: false,
_listening: false,
db: [Object] } ],
plugins: [],
models: {},
modelSchemas: {},
options: { pluralization: true } }
Server up on 80
1
1
Mongoose default connection disconnected
Mongoose default connection error: Error: connection closed
cat/var/log/mongodb/mongodb.log 在两台机器上显示完全相同的输出。
更新 1:安装程序突然开始正常工作,然后又停止了。我无法弄清楚是什么导致了这种情况发生。
最佳答案
我终于明白了,新设置使用的是更新版本的 nodejs。
当我从 7.x 迁移到 6.x 时,它运行良好。我猜 mongoose、node 7、mongodb 版本并没有很好地结合在一起。
关于mongodb Mongoose 连接打开回调没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41288374/