1

I've searched the Internet but couldn't find an answer for my problem. It seems specific to my application, but hopefully not :)

This error occurs both during heroku deployment as well as during dokku deployment.

But to the point.

I have the app.js file which part responsible for db connection looks like this:

/**
 * DB connection
 */
var dbUrl;
if(app.get('env') === 'development'){
    dbUrl = process.env.MONGOLAB_URI || "mongodb://localhost:27017/test";;
}
var db = require('./database');

db.connect(dbUrl, function(err){
    if(err) {
        console.log('Unable to connect to MongoDB');
    } else {
        console.log('MongoDB connected successfully!');
    }
});

I'm working on development environment only, so I've removed the production bits.

As you can see, I'm getting dbUrl from heroku environment variables, then I'm requiring db index file which looks like this:

var MongoClient = require('mongodb').MongoClient;

var state = {
    db: null
};

exports.connect = function(url, done) {
    if(state.db) return done();

    console.log("trying to connect to mongodb");
    console.log(url);
    MongoClient.connect(url, function(err, db){
        console.log("mongodb callback");
        if(err) return done(err);
        state.db = db;
        done();
    });
};

And than I'm invoking the db.connect method in the app.js file (first code snippet) from the database index file (just above).

And I'm getting this error:

Starting process with command NODE_ENV=development node server/bin/www

mongodb://user:password@ds011111.mongolab.com:33333/db_name

trying to connect to mongodb <<---- this is console.log from my db.connect method

/app/server/node_modules/mongodb/lib/server.js:228

   process.nextTick(function() { throw err; })

                    Error: connect ECONNREFUSED      

at exports._errnoException (util.js:746:11) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19) State changed from starting to crashed

The error occurs only during heroku or dokku deployment.

My db connection is 100% correct (all the environment variables are ok as well so please don't ask about it), checked it in shell, as well as connecting to it from my localhost within the app.

Basically it works on my localhost, I've deployed it on vps with ubuntu, end it works as well. The problems occurs only when I'm trying to use automation tools.

Any help greatly appreciated

Thanks!

matewilk
  • 1,191
  • 1
  • 13
  • 27
  • maybe try using an IP address instead of localhost? – Tony Barnes Aug 30 '15 at 14:07
  • I'm not sure if it's not clear enough, so I'll repeat - **"My db connection is 100% correct"** and **"I've deployed it on vps with ubuntu, end it works as well"** and `dbUrl = process.env.MONGOLAB_URI || "mongodb://localhost:27017/test";` - this line means that if variable is available, then it's chosen as the dbUrl variable and **"all the environment variables are ok as well so please don't ask about it"** but thanks for taking a look – matewilk Aug 30 '15 at 18:22
  • Yes that was perfectly clear in the question - thanks for repeating it. I actually suggested a different *way* of inputting the db url, rather than asking about the db connection or env variables. The reason for my suggestion is because I have seen a similar issue with *heroku* specifically, and using an IP address instead of localhost fixed it. Anyway, good luck. – Tony Barnes Aug 30 '15 at 18:33
  • Hello again, and thanks for replay, I'm not sure if I understand your suggestion in that case, the "mongodb://localhost:27017/test" is never reached on my heroku/dokku so I'm not sure what to change here. – matewilk Aug 31 '15 at 16:45
  • Can you give an example please? thanks – matewilk Aug 31 '15 at 16:52
  • I think it was something like this, but i'm not sure: `mongodb://127.0.0.1/test` – Tony Barnes Sep 01 '15 at 07:57
  • Sorry, but I really don't know where is your point ? Please give a FULL example. What this line even means ? Do you understand the problem at all? – matewilk Sep 01 '15 at 12:04
  • Of course I understand the problem. I am only trying to help you, but you are being pretty aggressive. I am talking about your `dbUrl` - it's in your code. Here is something similar to what I was suggesting: http://stackoverflow.com/a/24710577/1257504 Good luck. – Tony Barnes Sep 01 '15 at 13:19
  • Yes, I know you are trying to help, but I'm getting annoyed by you stating that you know what are you talking about but apparently you do not understand the problem, and it seems like you are not able to read the code properly, because if you could, you would know that `mongodb://127.0.0.1/test` is never used and this is not the problem. I was trying to emphasise it in the previous comments but you still don't get it. And the link you have posted just confirms what I've just stated. So please, read the code, comments, understand the problem and than answer. Thanks – matewilk Sep 02 '15 at 15:01
  • You are completely missing the point and being very insulting, judgmental and presumptuous. Good day sir. – Tony Barnes Sep 03 '15 at 09:32

0 Answers0