node.js - Node + Mongo : findOne OK but find().forEach DONT -


my problem seems linked asynchronous aspect of node, can't understand why.

this code works :

      expertsarray = ["expert1", "expert2"];       db.collection("users").findone({first_name: expertsarray[0]}, function(err, expert) {         console.log(expert.userid);         });       });  

this code bugs :

      expertsarray = ["expert1", "expert2"];       db.collection("users").find({ first_name: {$in: expertsarray} }, function(err, experts) {         experts.foreach(function(err, expert) {           console.log(expert.userid);           });         });       });  

in second case, experts exists , [object object], error :

error: [fatal] unhandled exception occured in bot typeerror: cannot read property 'userid' of null

i tried put first code loop, same bug appears... me understand ? lot :)

the parameters in foreach wrong.

according mozilla, arguments are:

currentvalue, index 

but have

err, expert 

so try instead:

experts.foreach(function(expert, expertindex) {   console.log(expert.userid);   }); }); 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -