Angular 删除用户及其所有文档Firestore
当用户想要删除他的帐户时,我要确保他在Firebase中创建的“文档”也被删除 我在网上找到了一些帮助,帮助我做到了这一点:Angular 删除用户及其所有文档Firestore,angular,typescript,firebase,google-cloud-firestore,Angular,Typescript,Firebase,Google Cloud Firestore,当用户想要删除他的帐户时,我要确保他在Firebase中创建的“文档”也被删除 我在网上找到了一些帮助,帮助我做到了这一点: deleteAccount() { const qry: firebase.firestore.QuerySnapshot = await this.afs.collection('houses', ref => ref.where('email', '==', this.afAuth.auth.currentUser.email)).ref.ge
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'关键字上得到一个错误,它说:'await'表达式只允许在异步函数中使用。
有谁能告诉我如何解决这个问题,或者有没有更好的方法
我对这个很陌生,所以我不知道如何继续,非常感谢您的帮助。解决方案是创建一个
async
函数
您提到您是编程新手,一开始很难理解承诺和异步/等待。我建议你看一下介绍
你可以用两种方法来解决它
Async deleteCount(){
顺便说一句,如果您使用async/Wait,请记住要很好地理解try/catch。祝您编码愉快!谢谢!这很有效,我现在尝试了第一个解决方案(添加async),但我肯定会尝试其他解决方案并观看视频。
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}`)