I’m trying to write a function that loops through an array of JSON objects and based on those values query mongoDB with mongoose. I’m trying to save Booking.find as an array of promises and then resolve all of them using Promise.all so that I can pass the result to another function.
However when I try to run the code I have right now it gets stuck at Promise.all and never moves past it. The database query works as intended and I have tried running the code with node version 15 and 16.
The strange thing is that the code works as intended on mac but not windows and ubuntu.
function checkBookings(timeslots, clinicID) {
const promises = [];
for (let i = 0; i < timeslots.length; i++) {
promises.push(
Booking.find({
clinic: mongoose.Types.ObjectId(clinicID),
date: new Date(convertDate(timeslots[i].date))
.toISOString()
.slice(0, 10),
startTime: timeslots[i].start + "-" + timeslots[i].end,
})
);
}
Promise.all(promises).then((bookings) => { //doesn't move past this point
timeslots = filterAvailabiltyZero(timeslots, bookings);
forwardTimeslots(timeslots, clinicID);
});
}
I have also tried to write the code with async/await but it has the same results.
async function checkBookings(timeslots, clinicID) {
const promises = [];
for (let i = 0; i < timeslots.length; i++) {
promises.push(
Booking.find({
clinic: mongoose.Types.ObjectId(clinicID),
date: new Date(convertDate(timeslots[i].date))
.toISOString()
.slice(0, 10),
startTime: timeslots[i].start + "-" + timeslots[i].end,
})
);
}
const bookings = await Promise.all(promises); //doesn't move past this point
timeslots = filterAvailabiltyZero(timeslots, bookings);
forwardTimeslots(timeslots, clinicID);
}