Connection time out during mongodb connect with node.js application

956 views Asked by At

I am trying to connect mongodb on my local machine & using below code, but it is giving time out error.

app.js->

// import the language driver
var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');
 var ObjectID = require('mongodb').ObjectID;
  // Connection URL
var url = 'mongodb://127.0.0.1:27017/testdb';

// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {

  //ensure we've connected
  assert.equal(null, err);
  console.log("Connected correctly to server");
  //close the database connection
  return db.close();

});

Error Trace:

C:\Users\AmitRuwali\git\myfinancenew>node app.js
Failed to load c++ bson extension, using pure JS version

C:\Users\AmitRuwali\git\myfinancenew\node_modules\mongodb\lib\mongodb\mongo_clie
nt.js:378
          throw err
          ^
AssertionError: null == { err: 'connection to [127.0.0.1:27017] timed out' }
    at C:\Users\AmitRuwali\git\myfinancenew\app.js:13:10
    at C:\Users\AmitRuwali\git\myfinancenew\node_modules\mongodb\lib\mongodb\mon
go_client.js:375:11
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

My Mongodb server is running fine in localhost and i am able to query the db & collections perfectly using mongo client.

I Need help regarding this timeout issue.

Thanks.

2

There are 2 answers

4
Saksijas On

try first

mongod -repair

mongod start

mongo

than start your app

If you are using windows, open mongoDB Databse server, and enter these 3 lines, after that try to start your app

if it is not working, uninstall than install mongo again using npm

0
ammu On

The problem is fixed, there is a issue in the version, I did below things as described in below URL and it worked.

Node MongoDb { err: 'connection to [127.0.0.1:27017] timed out' }

$ npm uninstall mongodb --save
$ npm install mongodb --save