2

I have a working Node.js app setup and running well when using my local development MongoDB with these settings and code:

var MongoDB     = require('mongodb').Db;
var Server      = require('mongodb').Server;

var dbPort      = 27017;
var dbHost      = 'localhost';
var dbName      = 'my-db';

var db = new MongoDB(dbName, new Server(dbHost, dbPort, {auto_reconnect: true}), {w: 1});
db.open(function(e, d){
if (e) {
    console.log(e);
}   else{
    console.log('connected to database :: ' + dbName);
}
});

But when I try to connect with my MongoHQ URI I am getting a Failed to Connect error.

I am unsure as how to use the URI: mongodb://username:password@linus.mongohq.com:10022/my-db with the above code.

Edit: Here is the next part of my code to show how I am using the db variable in case it helps with the problem:

var accounts = db.collection('accounts');

/* login validation methods */

exports.autoLogin = function(user, pass, callback)
{
accounts.findOne({user:user}, function(e, o) {
    if (o){
        o.pass == pass ? callback(o) : callback(null);
    }   else{
        callback(null);
    }
});
}

exports.manualLogin = function(user, pass, callback)
{
accounts.findOne({user:user}, function(e, o) {
    if (o == null){
        callback('user-not-found');
    }   else{
        validatePassword(pass, o.pass, function(err, res) {
            if (res){
                callback(null, o);
            }   else{
                callback('invalid-password');
            }
        });
    }
});
}
Ross J
  • 282
  • 7
  • 21

2 Answers2

0

Try this:

var mongodb = require('mongodb');
var url = require('url');

var MONGOHQ_URL = "mongodb://username:password@linus.mongohq.com:10022/my-db";

var connectionUri = url.parse(MONGOHQ_URL);
var dbName = connectionUri.pathname.replace(/^\//, '');

mongodb.Db.connect(MONGOHQ_URL, function(error, client) {
    if (error) throw error;

    console.log('Connected to database :: ' + dbName);

    // Some code - Print collection names
    client.collectionNames(function(error, names){
        if(error) throw error;

        console.log("Collections");
        names.forEach(function(colData){
            var colName = colData.name.replace(dbName + ".", '')
            console.log(colName);
        });                            
    });

});
Sobolev Eugene
  • 348
  • 1
  • 7
  • Thanks for the answer, I tried the code you suggested and got a lot of errors later on in my code, I have added an edit to the original post in case it helps. – Ross J Jan 25 '13 at 19:00
0
var username    = 'username';
var passwd      = 'password';


/* establish the database connection */

var db = new MongoDB(dbName, new Server(dbHost, dbPort, {auto_reconnect: true}), {w: 1});
    db.open(function(e, aa){
    if (e) {
        console.log(e);
    }   else{
           aa.authenticate(username, passwd,function(err2,d){
             if(d){
                 console.log("Database opened");
             }
             else{
                 console.log(err2);
             }
         });

    }
});