can't connect to mongolab with node.js on heroku
Asked Answered
E

3

7

I am having trouble making node.js and mongodb with mongolab work on heroku. I have read other issues like How do I setup MongoDB database on Heroku with MongoLab? and How do I manage MongoDB connections in a Node.js web application? but I still can not set up my connection. In the logs it says [Error: failed to connect to ...]

I have takend the db, host and port from the MONGOLAB_URI process env.I have the following code:

var mongoUri = mongodb://heroku_app17328644:{password}@ds037518.mongolab.com //taken from process.env.MONGOLAB_URI 

var host = 'mongodb://heroku_appXXXXXX:{password}@ds037518.mongolab.com';
var port = '37518';
var database = 'heroku_appXXXXXX';

Provider.db = new Db(database, new Server(host, port, { safe: true }, { auto_reconnect: true }, {}));
Provider.db.open(function(err, db){
console.log(db); //null
if (err) console.log(err);
else console.log('success');
});

What am I doing wrong ?

Euonymus answered 6/8, 2013 at 9:24 Comment(4)
What's the error message that you get?Varix
He gets Error: failed to connect to ... and the db is null :)Fugacious
That's not the full error message...Varix
[Error: failed to connect to mongodb://heroku_appXXXXXX:{password}@ds037518.mongolab.com] this is the error I am gettingEuonymus
B
14

The core issue seems to be that you're trying to use a MongoDB URI as a hostname.

Here's how to connect using a URI and MongoClient:

var mongodb = require('mongodb');
var uri = 'mongodb://user:pass@host:port/db';
mongodb.MongoClient.connect(uri, function (err, db) {
    /* adventure! */
});

Of course you'll want to substitute the user, pass, host, port, and db in the uri for your actual connect parameters. If you're using the MongoLab add-on for Heroku you can get the URI from the environment like this:

var uri = process.env.MONGOLAB_URI;

When using MongoClient safe mode is the default, so that option can be left out. To specify auto_reconnect simply pass it as a server option.

var mongodb = require('mongodb');
var uri = 'mongodb://user:pass@host:port/db';
mongodb.MongoClient.connect(uri, { server: { auto_reconnect: true } }, function (err, db) {
    /* adventure! */
});
Bagpipes answered 6/8, 2013 at 19:25 Comment(1)
Hi, I kind of have the same issue. When I do 'heroku config' it doesn't return the MONGODB_URI, it only returns the heroku link. I tried with this too 'heroku config:get MONGODB_URI' but it doesn't return anything. My code link is : github.com/Aritra1704/TutorialRestAPI/blob/master/server/db/… heroku push is working but the logs show failed to connect to port 27017. What might I be doing wrong? node version 8.9.4Dostie
Y
4

Here's is how I do it. This way, my application connects to the "test" database on my development machine and the "mongolab" database when deployed and running on Heroku.

mongoose = require("mongoose");
mongoURI = 'mongodb://localhost/test';
mongoose.connect(process.env.MONGOLAB_URI || mongoURI);
Yevette answered 11/11, 2014 at 1:20 Comment(0)
H
4

In my own case, I queried the configuration settings heroku config and it turns out that the mongodb is added as MONGODB_URI.

So, I added process.env.MONGODB_URI to the uri such as:

var uri = process.env.MONGODB_URI || process.env.MONGOHQ_URL || process.env.MONGOLAB_URI;
Haughay answered 16/8, 2016 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.