Firestore使用Firebase功能删除帐户删除中的所有用户数据 [英] Firestore delete all user data on account delete using a Firebase Function

查看:120
本文介绍了Firestore使用Firebase功能删除帐户删除中的所有用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Firebase/Firestore的新手,正在尝试创建一个Firebase函数,该函数将在删除Auth帐户后删除所有用户数据.

I'm new to Firebase/Firestore and trying to create a Firebase Function that will delete all user data upon deletion of an Auth account.

删除帐户成功调用了我的函数,并且我试图删除该用户的称为链接的集合,然后删除用户文档.但是我遇到了linksRef.forEach不是函数的错误.

My functions is successfully called on deleting an account and I'm trying to delete a collection called links for that user and then delete the user document. But I'm getting an error of linksRef.forEach is not a function.

有关如何级联删除的任何指导?

Any guidance on how I'd do this cascading delete?

exports.deleteUserData = functions.auth.user().onDelete((event) => {
  const userId = event.data.uid;

  const store = admin.firestore();

  store.collection('users').doc(userId).get().then(user => {
    if (user.exists) {

      user.collection('links').get().then(links => {
        links.forEach(link => {
          link.delete();
        })
        return;
      }).catch(reason => {
        console.log(reason);
      });

      user.delete();
      return;

    }    
    else {
      // User does not exist
      return;
    }
  }
  ).catch(reason => {
    console.log(reason);
  });  
});

推荐答案

基于@Doug Stevenson对Promises的评论,我设法通过将代码拼凑起来来完成这项工作.绝对不是最干净的,但是如果有人尝试做类似的事情,它就可以工作.

Based on the comment from @Doug Stevenson regarding Promises I managed to get this working by scraping together code. Definitely not the cleanest but it works if anyone is trying to do similar.

// Delete user data when user deleted
exports.deleteUserData = functions.auth.user().onDelete((event) => {
  const userId = event.data.uid;

  const database = admin.firestore();

  const linksRef = database.collection('users').doc(userId).collection('links');

  const deleteLinks = deleteCollection(database, linksRef, BATCH_SIZE)

  return Promise.all([deleteLinks]).then(() => {
    return database.collection('users').doc(userId).delete();
  });

});

/**
 * Delete a collection, in batches of batchSize. Note that this does
 * not recursively delete subcollections of documents in the collection
 */
 function deleteCollection (db, collectionRef, batchSize) {
  var query = collectionRef.orderBy('__name__').limit(batchSize)

  return new Promise(function (resolve, reject) {
    deleteQueryBatch(db, query, batchSize, resolve, reject)
  })
}

function deleteQueryBatch (db, query, batchSize, resolve, reject) {
  query.get()
  .then((snapshot) => {
          // When there are no documents left, we are done
          if (snapshot.size === 0) {
            return 0
          }

        // Delete documents in a batch
        var batch = db.batch()
        snapshot.docs.forEach(function (doc) {
          batch.delete(doc.ref)
        })

        return batch.commit().then(function () {
          return snapshot.size
        })
      }).then(function (numDeleted) {
        if (numDeleted <= batchSize) {
          resolve()
          return
        }
        else {
        // Recurse on the next process tick, to avoid
        // exploding the stack.
        return process.nextTick(function () {
          deleteQueryBatch(db, query, batchSize, resolve, reject)
        })
      }
    })
      .catch(reject)
    }

这篇关于Firestore使用Firebase功能删除帐户删除中的所有用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆