【问题标题】:Delete user and all his documents Firestore删除用户及其所有文档 Firestore
【发布时间】:2018-06-04 09:31:52
【问题描述】:

当用户想要删除他的帐户时,我想确保他在 Firebase 中创建的“文档”也被删除。

我在网上找到了一些帮助:

deleteAccount() {

        const qry: firebase.firestore.QuerySnapshot = await this.afs.collection('houses', ref => ref.where('email', '==', this.afAuth.auth.currentUser.email)).ref.get();
        const batch = this.afs.firestore.batch();

        qry.forEach( doc => {
          batch.delete(doc.ref);
        });
      batch.commit();         
}

但我在“等待”关键字上收到错误消息:'await' expression is only allowed within an async function.

谁能告诉我如何解决这个问题,或者是否有更好的方法?

我对此很陌生,所以我不确定如何继续,非常感谢任何帮助。

【问题讨论】:

    标签: angular typescript firebase google-cloud-firestore


    【解决方案1】:

    解决方案是创建一个async 函数。

    您提到您是编程新手,刚开始可能很难理解 Promise 和 async/await。我建议您观看this video 的介绍。


    你可以通过两种方式解决它。

    1. 异步,只需添加异步字:async deleteAccount() {
    2. 创建一个 Promise 链(实践一下):

    deleteAccount() {
      firebase.firestore.QuerySnapshot = await this.afs.collection('houses', ref => ref.where('email', '==', this.afAuth.auth.currentUser.email)).ref.get()
      .then(qry => {
        const batch = this.afs.firestore.batch();
        qry.forEach(doc => batch.delete(doc.ref));
        return batch.commit();
      })
      .then(() => console.log('done'))
      .catch(err => console.log(`failed with ${err.message}`)
    

    顺便说一句,如果您使用 async/await,请记住很好地理解 try/catch。编码愉快!

    【讨论】:

    • 谢谢!那行得通,我现在尝试使用第一个解决方案(添加异步),但我肯定会尝试其他解决方案并观看视频。
    猜你喜欢
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 2021-07-15
    • 2020-05-24
    • 2021-07-27
    • 2020-09-08
    相关资源
    最近更新 更多