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);
});
});
+3
source to share
1 answer
Check object "e", you may 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.
}
});
});
+3
source to share