2012-10-14 62 views
0

我试图连接我已经构建到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

回答

2

在连接建立之前,您的认证呼叫正在发送。您需要在“打开”回调中嵌套验证呼叫,像这样的应该工作:

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; 
    } 
    } 
}); 
相关问题