3

I am trying to get JSON from Mongodb using Express.js, and it returns "Undefined" in my console. Could you advise?

app.js:

var db = monk('localhost:27017/nodetest1', {
  username : 'USERNAME',
  password : 'PASSWORD'
});

index.js:

router.get('/url', function(req,res){

    var db = req.db;
    var collection = db.get('test1');

    collection.find({},{},function(e,docs){
    console.log(docs) // Returns "Undefined"
    res.send(docs);
    });
});
Taewan
  • 1,167
  • 4
  • 15
  • 25

1 Answers1

3

Check the "e" object, probably you have an error

router.get('/url', function(req,res){
  var db = req.db;
  var collection = db.get('test1');

  collection.find({},{},function(e,docs){
    if (!e) {
      console.log(docs); // Data you supposed to get in a correct way
    } else {
      console.log(e); // Checking the error - connection failure, bad authentication, etc.
    }
  });
});
Aleksandr
  • 2,185
  • 2
  • 21
  • 29
  • You can check for undefined variables as described in http://stackoverflow.com/questions/2647867/how-to-determine-if-variable-is-undefined-or-null – Aleksandr Dec 15 '14 at 12:41