mongojs icon indicating copy to clipboard operation
mongojs copied to clipboard

forEach is not working with my query.....??

Open Ashh007 opened this issue 10 years ago • 9 comments

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..

Ashh007 avatar Feb 25 '16 08:02 Ashh007

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

saintedlama avatar Feb 25 '16 09:02 saintedlama

ok thanks its working for me :)

Ashh007 avatar Feb 25 '16 09:02 Ashh007

@saintedlama I thought there was a forEach method in the cursor. It's even in the API

https://github.com/mafintosh/mongojs#cursorforeachfunction

sorribas avatar Feb 25 '16 09:02 sorribas

@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?

saintedlama avatar Feb 25 '16 16:02 saintedlama

i think the forEach callback is (err, value) and not (value)

mafintosh avatar Feb 25 '16 19:02 mafintosh

@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 avatar Feb 26 '16 04:02 Ashh007

@Ashh007 its function (err, doc)

mafintosh avatar Feb 26 '16 04:02 mafintosh

@mafintosh yes sir but in forEach we got only one value in function..

Ashh007 avatar Feb 26 '16 04:02 Ashh007

@Ashh007 this is how forEach is expected to work. If you prefer to get all documents just stick to the method outlined above.

saintedlama avatar Feb 29 '16 16:02 saintedlama