When a user wants to delete his account I want to make sure that the 'documents' he created in Firebase are also deleted.
I found some help online which got me to this:
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();
}
But I get an error on the 'await' keyword which says: 'await' expression is only allowed within an async function.
Can anyone tell me how to fix that, or if there's a better way of doing this?
I'm pretty new to this so I'm not sure how to proceed, any help is greatly appreciated.
The solution is to create an async
function.
You mentioned that you are new to programming, and in the beginning it can be difficult to understand promises and async/await. I would recommend that you watch this video for an introduction.
You can solve it in 2 ways.
async deleteAccount() {
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}`)
Btw, if you go with async/await, remember to understand try/catch well. Happy coding!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.