Home > Enterprise >  How can I delete a doc with a where query in Firebase v9?
How can I delete a doc with a where query in Firebase v9?

Time:05-09

I have a collection where it contains a couple of docs which all contains a id-field individual. But i can't figuere out how I can delete a specific document based on my query. I have tried with this:

 const deleteItem = async(item) => {
    const d = query(collection(db, 'allTasks'), where('id', '==', item.id));
    const docSnap = await getDocs(d);
    docSnap.forEach((doc) => {
      console.log(doc.data())
      deleteDoc(doc.data());
    });
  }

But i get the error: [Unhandled promise rejection: TypeError: t is not an Object. (evaluating '"_delegate" in t')]

Is this the wrong way or should i use batch? The console.log shows the right item that i have clicked delete on

CodePudding user response:

The deleteDoc() function take DocumentReference as parameter and not the document data.

docSnap.forEach((doc) => {
  deleteDoc(doc.ref); // and not doc.data()
});

Additionally, it might be a good idea to use a batch to delete documents to ensure they are all delete or none.

  • Related