Ashfaq Muhammad
Ashfaq Muhammad

Reputation: 790

MongoDB not connecting in nodejs and giving strange error?

I am trying to connect with my server and getting some results but It is giving me strange error.

Here is my code and error: Here is strange error:

E:\nodeJs>node mongoDBApi.js

E:\nodeJs\node_modules\mongodb\lib\mongo_client.js:792
          throw err;
          ^
[object Object]

Now here is complete error:

{ err: 'socketHandler',
  trace: '',
  bin: undefined,
  parseState:
   { sizeOfMessage: 759714643,
     bytesRead: undefined,
     stubBuffer: undefined } }

code:

var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://xxxx:[email protected]:22/';


MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("cdb");
  dbo.collection("ccollection").findOne({}, function(err, result) {
    if (err) throw err;
    console.info(result.name);
    db.close();
  });
});

Upvotes: 1

Views: 752

Answers (1)

Kontra
Kontra

Reputation: 93

Your err var is an object. You'd be better logging the err rather than throwing it, unless you already know it's properties.

eg.

> res = {name: 'hi', title: 'bye'}
> throw res
Thrown: [object Object]
> console.log(res)
{ name: 'hi', title: 'bye' }

Also, are you actually using x's in ''mongodb://xxxx:[email protected]:22/''? Cause that could be why you're getting socket connection failures.

Upvotes: 1

Related Questions