我正在尝试通过Firebase函数定期删除旧的通知。 我的数据库结构在Cloud Firestore中看起来像这样:
notification/autoID1/messages/autoID2/duration:Date1
/autoID3/duration:Date2
autoID4/messages/autoID5/duration:Date3
/autoID6/duration:Date4
我尝试了许多方法,但是没有一个起作用。这是一个示例:
exports.deleteoldnotifications = functions.https.onRequest((req: any, res: { status: (arg0: number) => { send: () => void; }; }) => {
const timeNow = new Date();
const db = admin.firestore();
const datatodelete:any= {};
db.collection('notification').get().then((snap: { forEach: (arg0: (collection: any) => void) => void; }) => {
snap.forEach((collection: any) => {
db.collection('notification').doc(collection.id).collection('messages').get().then((snapshot: { forEach: (arg0: (doccollection: any) => void) => void; }) => {
snapshot.forEach((doccollection) => {
const durations = new Date(doccollection.data().duration).getTime();
if (durations > timeNow) {
datatodelete[doccollection.id]= '/notification/'+collection.id+'/messages/'+doccollection.id;
}
});
});
});
if(Object.keys(datatodelete).length>0)
return db.doc(datatodelete).delete();
else
return res.status(200);
});
});
此操作超时。
所以,我的问题是:
如何同时删除多个文档,我在做什么错了?
谢谢!