3

I'am new to Node.js and MongoDB and I am trying it out.

I made a collection called footIco. When I query MongoDB in the console with db.footIco.find(), it return all the data.

However when I query MongoDB from Node.js it doesn't return any data.

I can see the connection in MongoDB server console.

Here is my Node.js script;

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
var url = 'mongodb://localhost:27017/footIco';

var findIco = function(db, callback) {
   var cursor =db.collection('footIco').find();
   cursor.each(function(err, doc) {
      assert.equal(err, null);
      if (doc != null) {
         console.dir(doc);
      } else {
         callback();
           console.dir(doc);
      }
   });
};

MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  findIco(db, function() {
      db.close();
  });
});

Can anyone tell me what is wrong with this. It's pretty much a copy and paste from the MongoDB tutorial

1
  • 1
    Would help if I was using the good database name. Commented Feb 19, 2016 at 15:55

1 Answer 1

1

This line:

assert.equal(err, null);

it is a JavaScript specific, that handlers that throws error will throw nowhere, I would recommend changing it to console.log(err) or just modify your callback to handle err parameter.

Sign up to request clarification or add additional context in comments.

3 Comments

But I'm still getting no result event if I remove that part.
If you get no error and no data this means you have no data, did you check mongo instance with some mongo console like robomongo?
Yeah its working i was calling the wrong database. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.