skip to Main Content

There is a collection users and inside user there is a document userID and inside this document there is another collection favourite and inside this collection , there is id’s of saved favourite data favouriteId.

So i want to delete that data inside favourite colletion.

My db look likes:-
users(collection)>userID(document)>favourite(collection)>favouriteId(document)

Here is my code for that:

  const deleteDoc = async (id) => {
    const userRef = collection(db, "users");
    const favRef = collection(
      userRef,
      `${firebaseAuth?.currentUser?.uid}/favourite`
    );
    const favId = doc(favRef, id);
    await deleteDoc(favId);
  };

favouriteId=id ,
userId=firebaseAuth?.currentUser?.uid

I use firebase v9.

2

Answers


  1. your code is Most of correct you just need some changings in code like that

    const deleteDocu = async (userId, favouriteId) => {
    const userRef = collection(db, "users");
    const favRef = collection(userRef, userId, "favourite");
    const favId = doc(favRef, favouriteId);
    await deleteDoc(favId);
    };
    
    Login or Signup to reply.
  2. You are naming your function in the same way as the Firebase function.

    Just change the function name

      const deleteDocu = async (id) => {
        const userRef = collection(db, "users");
        const favRef = collection(
          userRef,
          `${firebaseAuth?.currentUser?.uid}/favourite`
        );
        const favId = doc(favRef, id);
        await deleteDoc(favId);
      };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search