I have many arrays of values being sent from my view to the controller. Example:
year: ['2015','2016','2017']
cash: ['...','...','...']
.
.
.
0th index of each of these array forms one record in cashflow database table. 1st record another, and so on...cashflow is a collection inside "Property" model. I need to create these 'cashflows' iteratively whenever a record for 'property' is created.
I'm using sails framework. I am not able to create these records iteratively and render it all at once. I'm not sure if this needs to be done using async tasks.
Property.create(reqParams).exec(function(err, property) {
if (err) {
...
} else {
var index = 0;
var asyncTasks = [];
for (index = 0; index < reqParams.year.length; index++) {
asyncTasks.push(function(callback) {
CashFlowProjections.create({
year: reqParams.year[index],
belongsTo: property.id,
cash: reqParams.cash[index],
...)
.exec(function(err, cashflows) {
callback(err, cashflows);
});
});
}
async.parallel(asyncTasks, function(err, results) {
if (err) {
res.handleError(0, err);
} else {
res.view('property/preview', {
cashflows: results[0],
...
});
}
});
}
}
});
But the above code is wrong since the 'index' ends up being the same value for all the records and record creation fails.
I have also tried it without async tasks but I faced a problem of "res.view" getting executed before the creation of the cashflows records. Can someone guide me with this?