【发布时间】:2020-03-29 21:22:35
【问题描述】:
我是使用 async-await 编写打字稿的新手,但我检索了所有折扣,检查是否有效或过期,如果过期则获取所有折扣产品,然后在响应中打印其 ID,我无法处理嵌套承诺的问题和获取打折产品的 ID 以作为响应显示
exports.terminateDiscount = functions.https.onRequest(async (req, res) => {
try {
const discountSnapshots = await admin.firestore().collection("Discounts").get();
const promises = [];
const today = Date.now();
discountSnapshots.forEach(async discountSnapshot => {
const startDate = +discountSnapshot.data().startDate.toMillis();
const endDate = discountSnapshot.data().endDate;
if (today > startDate && endDate !== null && today > endDate.toMillis()) { // discount expired
promises.push(discountSnapshot.id); // print discount id only
const discountProducts=await admin.firestore().collection("Products").where("discountId", "==", discountSnapshot.id).get();
discountProducts.forEach(product => {
promises.push(product.id); // products id's not added in promises :(
});
}
});
res.send(await Promise.all(promises));
} catch (error) {
}
});
【问题讨论】:
标签: node.js typescript promise async-await google-cloud-firestore