I've been working on a timer-based function that is supposed to run once a week and create almost identical copies of existing documents. However, every time I try to execute it, I encounter the error message "Promises must be handled appropriately." I'm puzzled as to where I might have missed satisfying promises in my code structure. I have incorporated a return statement at each possible stage, so it's unclear to me what could be causing this issue.
Having launched approximately 40 cloud functions in the past, I thought I had a good grasp of how promises operate. Despite this experience, I seem to be overlooking something crucial in this scenario, and I can't pinpoint what exactly it is.
Below is the function in question:
exports.createRecurringDeals = functions.pubsub.schedule('0 0 * * 7').timeZone('Asia/Jerusalem').onRun((context) => {
db.collection('recurring_deals').get().then(querySnapshot => {
querySnapshot.forEach(bus => {
const businessListDoc = bus.data();
if (businessListDoc !== undefined) {
const dealsList = businessListDoc.list as Array<String>
return db.doc('businesses/' + bus.id).get().then(busDoc => {
const business = busDoc.data();
if (business !== undefined) {
dealsList.forEach(deal => {
return db.doc('deals/' + deal).get().then(snapshot => {
const oldDeal = snapshot.data();
if (oldDeal !== undefined) {
const promises: any = [];
const startTime = oldDeal.startTime + oldDeal.interval;
const endTime = oldDeal.endTime + oldDeal.interval;
const newDealDoc = db.collection('deals').doc();
const newDeal = {
id: newDealDoc.id,
business_ID: business.id,
business_name: business.name,
business_address_text: business.address_text,
business_address_lat: business.address_lat,
business_address_long: business.address_long,
business_phone_number: business.phone_number,
business_image: business.restaurant_photos[0],
business_categories: business.categories,
business_sub_categories: business.sub_categories,
discount: oldDeal.discount,
timestamp_start: startTime,
timestamp_end: endTime,
gmt: oldDeal.gmt,
amount: oldDeal.amount,
claimers: [],
active: true
};
promises.push(newDealDoc.set(newDeal));
promises.push(db.doc('recurring_deals/' + business.id).update({ list: FieldValue.arrayRemove(oldDeal.id) }));
promises.push(db.doc('recurring_deals/' + business.id).update({ list: FieldValue.arrayUnion(newDeal.id) }));
return Promise.all(promises);
} else {
return null;
};
});
});
return null;
} else {
return null;
};
});
} else {
return null;
};
});
return null;
});
});