如何通过函数从firebase中删除数据?
我正在尝试通过函数从firestore中删除旧的通知。我不知道如何将所有文档及其子集合放入集合中。
db树看起来像这样:
编辑:
这是我试过的:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.deleteoldnotifications = functions.https.onRequest((req: any, res: { status: (arg0: number) => { send: () => void; }; }) => {
const timeNow = new Date();
const db = admin.firestore();
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);
if (durations <= timeNow) {
doccollection.delete();
console.log('duration: ',doccollection.duration);
}
});
});
});
});
return null;
});
该功能以超时结束。
怎么去扔通知集合并删除旧通知集合?
回答如下:没有像functions.firestore
这样的东西,这正是错误信息所说的。
如果要在未从Firestore本身触发的功能中访问Firestore,请使用Admin SDK执行此操作。您已在代码中导入Admin SDK,因此只需:
admin.firestore().doc(...)