Taewan
Taewan

Reputation: 1227

Express.js - Mongo object returns Undefined

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);
    });
});

Upvotes: 3

Views: 984

Answers (1)

Aleksandr
Aleksandr

Reputation: 2321

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.
    }
  });
});

Upvotes: 3

Related Questions