mongodb mongoose连接打开回调没有被调用

时间:2016-12-22 17:06:28

标签: mongodb mongoose mean-stack

我有一个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恢复数据库设置看起来很好。

但是,启动服务器时,不会调用connection-open回调。如果我停止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:设置开始正常工作,蓝色再次停止。我无法弄清楚是什么让这种情况发生。

1 个答案:

答案 0 :(得分:0)

我终于弄明白了,新的设置是使用更新版本的nodejs。

当我从7.x移动到6.x时它工作正常。我猜mongoose,节点7,mongodb版本并没有很好地结合在一起。