2

When trying to start Node.js app hosted on Heroku with MongoLab addons, I got the following error on Heroku logs when connecting to MongoDB. It looks like it is trying to connect to a local mongo even though I already set the heroku environment variable.

My entire code is here on GitHub: https://github.com/yhagio/meetup_planner

My gist for this issue

Heroku logs

2015-11-28T22:22:58.460531+00:00 heroku[web.1]: Starting process with command `node server.js`
2015-11-28T22:22:59.353911+00:00 app[web.1]: Server started at port number:  38701
2015-11-28T22:22:59.303795+00:00 app[web.1]: Connecting to DB :  mongodb://USERNAME:PASSWORD@ds059804.mongolab.com:59804/heroku_tb6frdh6
2015-11-28T22:22:59.371719+00:00 app[web.1]:
2015-11-28T22:22:59.371723+00:00 app[web.1]: /app/node_modules/mongodb/lib/server.js:235
2015-11-28T22:22:59.371725+00:00 app[web.1]:         process.nextTick(function() { throw err; })
2015-11-28T22:22:59.371725+00:00 app[web.1]:                                       ^
2015-11-28T22:22:59.371729+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:27017
2015-11-28T22:22:59.371730+00:00 app[web.1]:     at Object.exports._errnoException (util.js:860:11)
2015-11-28T22:22:59.371731+00:00 app[web.1]:     at exports._exceptionWithHostPort (util.js:883:20)
2015-11-28T22:22:59.371732+00:00 app[web.1]:     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
2015-11-28T22:23:00.052255+00:00 heroku[web.1]: Process exited with status 1
2015-11-28T22:23:00.880672+00:00 app[web.1]: Connecting to DB :  mongodb://USERNAME:PASSWORD@ds059804.mongolab.com:59804/heroku_tb6frdh6
2015-11-28T22:23:00.939388+00:00 app[web.1]:
2015-11-28T22:23:00.939392+00:00 app[web.1]: /app/node_modules/mongodb/lib/server.js:235
2015-11-28T22:23:00.939393+00:00 app[web.1]:         process.nextTick(function() { throw err; })
2015-11-28T22:23:00.939397+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:27017
2015-11-28T22:23:00.939394+00:00 app[web.1]:                                       ^
2015-11-28T22:23:00.939398+00:00 app[web.1]:     at Object.exports._errnoException (util.js:860:11)
2015-11-28T22:23:00.939399+00:00 app[web.1]:     at exports._exceptionWithHostPort (util.js:883:20)
2015-11-28T22:23:00.939400+00:00 app[web.1]:     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
2015-11-28T22:23:00.917276+00:00 app[web.1]: Server started at port number:  42573
2015-11-28T22:23:01.626965+00:00 heroku[web.1]: State changed from starting to crashed
2015-11-28T22:23:01.614846+00:00 heroku[web.1]: Process exited with status 1
2015-11-28T22:23:13.224552+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=warm-retreat-5113.herokuapp.com request_id=54404d8a-7744-49ae-b744-4417abd4b347 fwd="166.62.227.154" dyno= connect= service= status=503 bytes=
2015-11-28T22:23:13.581322+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=warm-retreat-5113.herokuapp.com request_id=063c0fef-8260-46b5-a0f6-b4d546e9a60b fwd="166.62.227.154" dyno= connect= service= status=503 bytes=

Here is the code where I making a connection to MongoDB (MongoLab addons from Heroku) server/db_connection.js

var mongoose  = require('mongoose');
var config    = require('../express-config');
var userModel = require('./models/user');
var eventModel = require('./models/event');

function makeDefaultConnection() {
  var uri = process.env.MONGOLAB_URI || '127.0.0.1/meetupplanner';
  console.log('Connecting to DB : ', uri);

  var conn = mongoose.createConnection(uri);

  conn.on('error', function(err){
    console.log('Connection Error ::: ', err);
  });

  conn.model('User', userModel.userSchema);
  conn.model('Event', eventModel.eventSchema);
  return conn;
}

module.exports.defaultConnection = makeDefaultConnection();

Profile

web: node server.js

In Server.js

...
var port = process.env.PORT || 3000;
app.listen(port, function() {
  console.log('Server started at port number: ', port);
});

After I added MongoLab Add-ons on Heroku I verified that my MONGOLAB_URI is set on Heroku (Settings > Config Variables)

Reference: https://devcenter.heroku.com/articles/mongolab

Similar issues asked on StackOverflow:

Note that I could connect to mongo shell via:

$ mongo ds059804.mongolab.com:59804/heroku_tb6frdh6 -u USERNAME -p PASSWORD

I think credentials are fine.

Community
  • 1
  • 1
YHDEV
  • 477
  • 8
  • 22
  • It looks like something is wrong with your config variables, despite your claim to have that correct. I think you will also face problems with `conn.model('User', userModel.userSchema)` down the line as I think model is a method on mongoose, not on mongoose.connect – Simon H Nov 28 '15 at 14:20
  • @SimonH Oh sorry I modified the `connect` to `createConnection`, it works locally but not on Heroku. I removed and re-added addon but still same issue. since I can login to mongo shell with credentials, configuration seems fine though ... – YHDEV Nov 28 '15 at 14:47
  • If you look at the time stamps I wonder whether the code you think is causing the problem actually is. The references to localhost seem to come a bit later than the console.log o fthe connect attempt – Simon H Nov 29 '15 at 07:01

2 Answers2

3

I finally figured out. It was the middleware configuration of connect-mongo with session.

In my config, it was:

app.use(session({
  ...

  // ===== Following 3 lines are trying to connect to local db =====
  store: new MongoStore({    
    'db': 'meetupplanner'
  }),

  // ===== So, I replaced 3 lines above with following =====
  store: new MongoStore({
    url: process.env.MONGOLAB_URI
  }),

  ...
HyderA
  • 20,651
  • 42
  • 112
  • 180
YHDEV
  • 477
  • 8
  • 22
0

As a next step, why don't you do this so that you are sure that the config variable is making it through

var uri = process.env.MONGOLAB_URI || config.dbHostName;
console.log('===== Connecting to DB ... =====', uri);
var conn = mongoose.createConnection(uri,...
Simon H
  • 20,332
  • 14
  • 71
  • 128
  • Updated the code, but this does not change anything for the connection error. Thanks for the suggestion though. – YHDEV Nov 28 '15 at 17:57
  • All I can suggest is that run the code locally but ensure that it points towards Mongolab and turn of local mongo. I'm wondering whether it is some other line that is causing the problems – Simon H Nov 28 '15 at 18:04
  • Yeah I tried the same MongoLab locally, app runs no problem. Since error message says `Error: connect ECONNREFUSED 127.0.0.1:27017` it just doesn't work on Heroku. I requested a support ticket on Heroku & MongoLab, so I'll wait while trying something else. – YHDEV Nov 28 '15 at 18:11
  • @Yuichi Did you recieve any update or how you fixed that problem. I need your help. – Muhammad Usama Rabani Aug 03 '21 at 11:05