mahmoud mahmoud - 3 months ago 17
Javascript Question

How to access a specific field in Expressjs returned by mongodb collection.find method?

I'm Trying to access one field returned from mongodb collection.find method
, I can't do that and nothing is showing on the console.log

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

collection.find({buildingNO:"1"},{},function(e,docs){
var x=docs[0].price;
console.log(x);
});
});


Note: I'm using monk middle-ware not native mongodb

thanks

Answer

Check the error argument in the callback and your return argument is:

x=docs[0]...

And not:

x=doc[0]

I'm surprised you don't get an undefined variable error.