Home > Blockchain >  Valid way to delete a doc from firebase
Valid way to delete a doc from firebase

Time:10-19

I want to delete a game from my firestore collection, but i get error:

TypeError: doc is not a function

I am using the latest version of Firebase. What is the proper way to delete the doc?

import {where,query,deleteDoc,collection, doc, getDocs, getFirestore } from "firebase/firestore";
    
deleteGame(game)  {            
  const db = getFirestore();
  const q = query(collection(db, "history"), where("date", "==", game.date));
  const doc = getDocs(q);
  const quer = await getDocs(q);

  quer.forEach((doc) => 
   {    
       deleteDoc(doc(db, "history", doc.id));
   });
}

CodePudding user response:

According to firebase documentation for delete data you should indeed use

deleteDoc(doc(db, "history", doc.id));  

But doc needs to be the function imported from firebase/firestore . You are rewriting the value of doc with the element from quer ( quer.forEach((doc) => ).

You also have const doc = getDocs(q); so you will need change the name of both doc variables in order to use the imported function inside the forEach callback.

Also keep in mind that this won't subcollections (if you have any - as specified in the docs).

  • Related