最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何通过函数从firebase中删除数据?

运维笔记admin8浏览0评论

如何通过函数从firebase中删除数据?

如何通过函数从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(...)
发布评论

评论列表(0)

  1. 暂无评论