firebase - 如何从 auth.user.onDelete 触发器中删除文档集合和所有嵌套数据

标签 firebase google-cloud-firestore google-cloud-functions

目前,删除用户数据的逻辑如下:

import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';

const firestore_tools = require('firebase-tools');

admin.initializeApp();

const Auth = admin.auth();
const UsersCollection = admin.firestore().collection(`users`);

exports.deleteUserDocuments = functions.auth.user().onDelete((user) => {
  const userID = user.uid;

  UsersCollection.doc(userID)
      .delete({})
      .catch(error => {
        return error
      });
});

但是由于用户文档记录包含包含其他文档和集合的嵌套集合,因此它们仍然被保留:When you delete a document, Cloud Firestore does not automatically delete the documents within its sub-collections
我进行了一些研究,并找到了有关如何创建可调用函数的文档:
https://firebase.google.com/docs/firestore/solutions/delete-collections

但我想知道是否可以从 auth.user.onDelete 执行这个逻辑扳机?

更新解决方案
const firestore_tools = require('firebase-tools');

exports.deleteUserDocuments = functions.auth.user().onDelete((user) => {
    const userID = user.uid;
    const project = process.env.GCLOUD_PROJECT;
    const token = functions.config().ci_token;
    const path = `/users/${userID}`;

    console.log(`User ${userID} has requested to delete path ${path}`);

    return firestore_tools.firestore
        .delete(path, {
            project,
            token,
            recursive: true,
            yes: true,
        })
        .then(() => {
            console.log(`User data with ${userID} was deleted`);
        })
});

最佳答案

您可以在您想要的任何触发器中运行您想要的任何代码。触发器的类型与您可以运行的代码类型没有任何关系。

关于firebase - 如何从 auth.user.onDelete 触发器中删除文档集合和所有嵌套数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60806147/

相关文章:

swift - 如何知道 fetchProviders() 何时完成执行?

firebase - 如何在 flutter 中检查集合是否存在于 firestore 中?

javascript - 如何在 Cloud Functions 中解析/返回 forEach 函数

firebase - I/flutter(22027): MissingPluginException(No implementation found for method DocumentReference#setData on channel plugins. flutter.io/cloud_firestore)

node.js - Stripe API 未处理的拒绝响应错误

android - Flutter:如何在整个应用程序中设置观察者

java - Firebase : Can't convert object of type java. lang.String 输入 com.example.g.Model.Cart

angular - AngularFire2 : how to handle $key 中的类

google-cloud-functions - 错误 : No handler for requested intent at WebhookClient. handleRequest

android - 使用 Firestore 时如何为 RecyclerView 添加搜索过滤器?