This is what I want: I have a large array called results. Each item in that array I want to put in my iteration function and have it saved to my mongoDB (I omitted some steps here as well which are not important). After every single one of those items has been saved to the database (or refused) I want console.log to display 'All done'. Unfortunately All done is displayed almost immediately after calling the series while everything else is still processing. How do I do this right?
I am using mongoose with a model called 'light' and for readability I omitted all err messages from the code. I use node-async for the eachSeries part
var async = require('async');
var results = [very big array]
var iteration = function(row,callbackDone) {
light.find({data: row.data}, function (err,entry) {
if(entry.length) {
callbackDone();
return console.log(entry + ' already exists');
}
var newEntry = new light(row);
newEntry.save(function (err,doc) {
console.log(entry + ' saved');
callbackDone();
});
});
};
async.eachSeries(results,iteration, function (err) {
console.log('All done');
});