我试图阅读类似问题的答案,但无法解决我的功能。
我正在构建优惠券应用程序,现在我正在研究重新获得优惠券的功能。我设计它们的方式是,使功能每周一次遍历应重复使用的优惠券列表,并在下一周创建一个类似的优惠券。 然后,先前的优惠券将从列表中删除。
我目前已将功能设置为每2分钟检查一次功能,但我不断收到此错误消息
Function returned undefined, expected Promise or value
随后出现此错误:
TypeError: Cannot read property '0' of undefined
,但我不明白为什么。
这是我的功能:
exports.createRecurringDeals = functions.pubsub.schedule('every 2 minutes').timeZone('Asia/Jerusalem').onRun((context) => {
db.collection('recurring_deals').get().then(querySnapshot => {
querySnapshot.forEach(bus => {
const businessListDoc = bus.data();
if (businessListDoc !== undefined) {
return db.doc('businesses/' + bus.id).get().then(busDoc => {
const business = busDoc.data();
if (business !== undefined) {
businessListDoc.list.forEach((deal:string) => {
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).catch(err => console.log(err));
} else {
return null;
};
}).catch(err => console.log(err));
});
return null;
} else {
return null;
};
}).catch(err => console.log(err));
} else {
return null;
};
});
return null;
}).catch(err => console.log(err));
});
这是我的数据的结构方式:
公司文件:
交易文件:
然后是重复交易列表的文档。请注意,这些文档与其所属的公司具有相同的ID:
我一直在浏览该函数,所有数据似乎都放在正确的位置,没有拼写错误,那为什么它不起作用?
答案 0 :(得分:3)
问题很可能在以下一行:
business_image: business.restaurant_photos[0]