Near the end of the look where you see console.log(authors), the array is constantly overwriting itself. The async part of this loop is usually outside the loop. I did this to debug the reason why I get a big array full of the same username, rather than an array full of a series of different usernames. This is the code I have:
while (i >= 0) {
var search = User.find({'twitter.id' : collected[i].author});
search.limit(1);
searches.push(function(cb) {
search.exec(function (err, hold){
if (err) cb(err, null);
else {
cb(null, hold[0].twitter.username);
}
});
});
i = i - 1;
async.parallel(searches, function( err, authors) {
if ( err ) return console.error( err );
else {
console.log(authors);
}
});
}
These are the results I get:
It's been a long day, I'm not quite seeing where I'm going wrong.