连接到节点上的MongoHQ,错误



我正在尝试将我构建的应用程序连接到MongoHQ数据库。

这是代码:

mongo = require('mongodb')
Server = mongo.Server
Db = mongo.Db
BSON = mongo.BSONPure;
con = null;
server = new Server('staff.mongohq.com', 'THE_PORT', {auto_reconnect: true});
DBCon = new Db('THE_DB', server, {safe: false});
DBCon.authenticate('test_user', 'test_pass', function() {});
DBCon.open(function(err, db) { if(!err) { con = db; } });

我有在MongoHQ中创建的数据库和用户。当我从命令行连接时,一切正常。

但是当我运行我的应用程序时,我收到此错误:

return this.connectionPool.getAllConnections();
TypeError: Cannot call method 'getAllConnections' of undefined

它无法连接到数据库。但是当我在没有身份验证的情况下连接到我的本地数据库时,它工作正常。

那么错误是什么,我应该如何解决它?

谢谢! :D

在建立连接之前发送身份验证调用。您需要将身份验证调用嵌套在"开放"回调中,这样的事情应该可以工作:

mongo = require('mongodb')
Server = mongo.Server
Db = mongo.Db
BSON = mongo.BSONPure;
con = null;
server = new Server('staff.mongohq.com', 'THE_PORT', {auto_reconnect: true});
DBCon = new Db('THE_DB', server, {safe: false});
DBCon.open(function(err, db) {
  if(!err) {
    db.authenticate('test_user', 'test_pass', function(err){
      if(!err) con = db;
    }
  }
});

最新更新