So I have this code to check if a date in the database has an event. I want to set the hasEvent to true in the array of weeks and days. The problem is at the end it logs the same line 4 times. Why does it do this and how could I go about solving it?
This is the code I currently use:
for (var i = 0; i < weeks.length-1; i++) {
for (var j = 0; j < weeks[i].days.length-1; j++) {
var datum = weeks[i].days[j];
db.collection('events').doc(datum.year + "-" + datum.month + "-" + datum.day).get().then(function(doc) {
if (doc.exists) {
console.log(datum.year + "-" + datum.month + "-" + datum.day + " is true" + " i " + i + " j " + j);
weeks[i].days[j].hasEvent = "true";
}else{
weeks[i].days[j].hasEvent = "false";
}
});
}
}
It keeps returning 2018-04-28 is true i 5 j 6 I use the firestore database. the collection is called events. in events I have a document that has the date as name and in there I have some info.