I'm fairly new to node.js and sails.js but not to javascript I have a simple function that iterates over an array of abjects, validates them, each validated object in the array is pushed into another array with i want to pass onto my my view.
if(results){
var items = results.one;
var toReturn = [];
for(var i = 0; i<items.length; i++){
var item = items[i];
Inventory.findOne({primaryKey: item.primaryKey}, function(err, found){
if(err){
res.send("Err reached");
}
else{
//Some validation code
if (validations_failed_condition){
console.log("FAiled validations");
}
else{
console.log("Validations passed, Pushing item");
console.log(item);
toReturn.push(item);
}
}
});
}
res.send(toReturn);
}
Even though my logs are showing me multiple items are being validated and being pushed into my array toReturn. The data being sent to the view is an empty array.
Is for looping in node.js asynchronous ? Is data being passed to the view before the for loop is completed ?
What can I do to fix this ?