My Setup: NodeJS, express, mongoose
var i;
for(i = 0; i < results.length; i++){
console.log("out: "+i);
RegionData.findOne({'rid': results[i].region_id}, function (err, product) {
if (product) {
console.log("in: " + i);
}
});
}
The output:
out: 0
out: 1
out: 2
out: 3
in: 4
in: 4
in: 4
in: 4
My expect output:
out: 0
in: 0
out: 1
in: 1
out: 2
in: 2
out: 3
in: 3
I don't know why it is not my expect output, it finishes the "out: i" first then "in: i". Whether is the .findOne problem?