Inability to connect to mongodb (mongolab) from nodejs app using localhost

I am trying to connect using the following code.
I am running nodejs app locally and I am unable to connect to MongoLab database using native MongoDb driver.
After about 30 seconds, an error is returned :
{"name": "MongoError", "message": "no valid seed servers in list"}

Should I declare something else somewhere? What am I missing?
When I run it live on Heroku, it connects fine.
Also, when I run it from terminal (shell), it connects fine.

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

   var auth = {
     user: 'root',
     pass: 'blabla',
     host: 'blabla.mongolab.com',
     port: 63879,
     name: 'heroku_blabla'
   };

        // Connection URL
        var url = util.format('mongodb://%s:%s@%s:%d/%s',
            auth.user, auth.pass, auth.host, auth.port, auth.name);

        // Use connect method to connect to the Server
        MongoClient.connect(url, function(err, db) {
            assert.equal(null, err);
            console.log("Connected correctly to server");

            db.close();
        });   

      

+3


source to share


1 answer


I solved it, but I don't know what caused the problem.
I suspected it was a DNS issue.

So, I went to terminal and first ran:

ping  ds00000.mongolab.com

      

(change 00000 to the port of your MongoLab database)



to check the host.

Then I run: nc -w 3 -v ds000000.mongolab.com 000000

Finally, if these two succeed, then it is not a port issue. So then I changed the hostname in my code from the IP of that hostname in mongoLab and resolved this error.
Note. The IP address of the mongoLab DB node appears in the response of the second command in your terminal.

+4


source







All Articles