forEach is not working with my query.....??
Hi Sir,
db.collection.find( { } ).forEach(function(docs){
console.log(docs);
})
this query is not working.. while applying only find then its working... please provide me some solution on it..
db.collection.find( { } ) returns a cursor which has no forEach function. To make this query working you could use
db.collection.find( { } ).toArray(function (err, docs) { docs.forEach ... })
or simply
db.collection.find( { }, function(docs) { docs.forEach ... })
Or use the cursor API https://github.com/mafintosh/mongojs#cursor
ok thanks its working for me :)
@saintedlama I thought there was a forEach method in the cursor. It's even in the API
https://github.com/mafintosh/mongojs#cursorforeachfunction
@sorribas You got me :smile: there is a forEach function defined in the cursor prototype. @Ashh007 What's your exact issue with the forEach on the cursor? Any error message?
i think the forEach callback is (err, value) and not (value)
@saintedlama while using in mongo console its working withe following code...
db.collection.find( { } ).forEach(function(docs){ console.log(docs); })
but not working with mongojs... getting empty response..
@Ashh007 its function (err, doc)
@mafintosh yes sir but in forEach we got only one value in function..
@Ashh007 this is how forEach is expected to work. If you prefer to get all documents just stick to the method outlined above.