Reputation: 95
I am trying to validate a booking by comparing clashes with other bookings (called individually from the db) in a trainer's timetable in a booking site.
I know Firebase calls are asynchronous therefore I need to find a way to wait for all bookings inside the forEach function to be fetched and validated.
I tried placing a flag variable before the forEach and console.logged it at the end of it, but obviously it didn't work as the console.log wouldn't wait for the forEach to complete before running.
I've read about 'async await' but it seems like it would be over-kill for this scenario (?). Could there be an easier way to do this?
Any help appreciated.
const bookingData = {
coursename:this.state.coursename,
location:this.state.location,
trainerid:this.state.trainerid,
startDatetime: this.state.startDatetime,
endDatetime: this.state.endDatetime,
} //FORM DATA I WANT TO VALIDATE
db.collection('timetables').doc(timetableid).get().then(timetable=>{
const data = timetable.data(); //ARRAY OF BOOKING ID'S
data.bookings.forEach(bookingid=>{
db.collection('bookings').doc(bookingid).get().then(bookingref=>{
//FOR EACH 'BOOKING' DOCUMENT IN MY DB, I WANT TO PERFORM THE FOLLOWING OPERATION
const booking = bookingref.data().bookingInfo;
if( booking.startDatetime.toDate() <= bookingData.startDatetime &&
booking.endDatetime.toDate() >= bookingData.startDatetime &&
booking.startDatetime.toDate() <= bookingData.endDatetime &&
booking.endDatetime.toDate() >= bookingData.endDatetime) {
console.log('TIME SLOT UNAVAILABLE')
}
else {
console.log('TIME SLOT AVAILABLE')
}
}).catch(err=>console.log(err));
});
})
// FIND A WAY TO SEE IF THE BOOKING WAS VALID AFTER BEING COMPARED WITH ALL OF THE BOOKINGS IN THE DB
Upvotes: 2
Views: 70
Reputation: 4783
forEach
to a map
db
call inside the map.map
will be an array of promises, resolving to all the booleans.Promise.all
then
after that. It will receive the array of booleans.Code:
Promise.all(
data.bookings.map(
booking => db....get().then(bookingRef => {
// return true or false based on your condition
})
)
).then(results => {
// this will wait for all the db calls to complete.
// and you get all the booleans in the results array.
const isAvailable = !results.includes(false);
});
Upvotes: 1