I am trying to query a Firebase Cloud Firestore asynchronously for each item in an array. My understanding of the async.map function was that it would perform a function on each item in an array, and only fire its callback once all of the items have been processed. However, the callback below gets executed immediately after the first query, and before any results are available. What's the best way to do this?
var db = admin.firestore();
var getData = function (item, doneCallback) {
db.collection('myCollection').doc(item).get()
.then(results => {
console.log("Callback here");
return doneCallback({
"name": results.data().name,
"id": results.data().id,
"phone": results.data().phone,
});
});
};
async.map(myArray, getData, function (err, results) {
console.log("Finished");
for (var i=0;i<results.length; i+=1){
console.log(results[i].name);
console.log(results[i].id);
console.log(results[i].phone);
}
});