我正在尝试编写基于计时器的函数。它应该每周运行一次,并创建几乎所有现有文档的副本。 每次尝试启动它时,我都会不断收到“必须正确处理承诺”错误,但是我不明白我做错了什么?在任何阶段都有回报。 哪里有我不满足的承诺?
我以前已经启动了大约40种云功能,所以我对诺言如何工作有一定的了解,但是我显然在这里缺失了一些东西,而对于我的一生,我无法理解它是什么。 / p>
这是功能:
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;
});
});
答案 0 :(得分:1)
我猜测它希望您在.catch()
之后添加.then
子句,以防Promise出于任何原因失败。因此,例如,尝试更改
return db.doc('deals/' + deal).get().then(snapshot => {
...
});
到
return db.doc('deals/' + deal).get().then(snapshot => {
...
}).catch(err => console.log(err));
答案 1 :(得分:1)
让我们以这样的想法开始:每个承诺都必须由.catch
或.then
处理。
因此,您缺少的第一件事是即将结束的Promise.all()
调用的处理程序。
此外,在最新版的JS中,您必须 在一个pomise之后放置一个catch
块来处理拒绝,即使它什么也不做,即使您有{{ 1}}。
在这里,您错过了.then
的主要承诺(.catch
)和db.collection('recurring_deals').get()
的通话。
添加这些内容,错误应消失。