Javascript 云存储删除功能
全部, 我正在使用Firestore作为后端,并试图编写一个将在每个月的第一天运行的云函数。此函数需要删除函数运行日期之前的所有数据库条目。我能够完成以下函数,但它不会删除任何条目。也许有人能帮我解决这个问题Javascript 云存储删除功能,javascript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Google Cloud Firestore,Google Cloud Functions,全部, 我正在使用Firestore作为后端,并试图编写一个将在每个月的第一天运行的云函数。此函数需要删除函数运行日期之前的所有数据库条目。我能够完成以下函数,但它不会删除任何条目。也许有人能帮我解决这个问题 export const deleteOldPrayerRequests = functions.pubsub.schedule('0 0 1 * *').onRun(async (context) => { const date = new Date(); cons
export const deleteOldPrayerRequests = functions.pubsub.schedule('0 0 1 * *').onRun(async (context) => {
const date = new Date();
console.log('---> Timestamp', context.timestamp);
console.log('---> Date Today', date);
console.log('---> Date Today', date.setDate(date.getDate()));
console.log('---> Date 14 days ago', date.setDate(date.getDate() - 14));
const snapshot = await admin.firestore().collection('prayerRequests').get();
snapshot.docs.forEach(doc => {
const ts = doc.get('dateSubmitted');
if (date.setDate(date.getDate() - 14) >= ts.toMillis()) {
console.log(doc.data());
doc.ref.delete().then((data: any) => {
console.log(data);
}).catch((error: any) => {
console.log(error);
});
}
});
});
这是Firestore官方文档中提供的用于删除文档的示例代码段
db.collection("cities").doc("DC").delete().then(function() {
console.log("Document successfully deleted!");
}).catch(function(error) {
console.error("Error removing document: ", error);
});
参考文献
您需要返回一个承诺,该承诺在所有异步工作完成时解析 完成,否则云功能将提前终止道格 史蒂文森
我也有过类似的问题。为了使删除生效,需要使用回调函数。 随着Angular的更新,处理它的方式似乎在不断变化。这是我使用Angular 8(最新版本)的解决方案:
希望这对您有用。您需要返回一个承诺,该承诺在所有异步工作完成时解决,否则云函数将提前终止所有异步工作。@Doug Stevenson您能将其作为答案发布吗?从你的投票结果来看,我相信这是有帮助的。
this.db.collection('prayerRequests')
.get()
.subscribe((snapshot) =>{
snapshot.forEach(doc => {
this.db.collection('dateSubmitted').doc(doc.id).delete()
});
})