menu

Questions & Answers

How can i delete this collection in firebase?

i recently began to use firebase and i ran into a problem, to get it straight, i want to make a e-commerce site, ofcourse it has cart, i used firebase for my back-end, for every user, i create a document in "users" collection, each user's document id is the uid of that given user. On every document, i have a cart, and in the cart, the products, on my cart page, i want to have a "Empty Cart" that deletes all the products, meaning it deletes "cart" collection. How can i do that? I have left some photos below that will probably explain the problem better.

Image of the firebase tree

I tried this and it did not work!

const deleteCart = async () => {
    const cartDoc = `users/${user.uid}/cart`
    
    await deleteDoc(db, cartDoc)
    
    }

i get this error : " TypeError: Cannot use 'in' operator to search for '_delegate' in undefined "

Comments:
2023-01-24 23:25:14
If you want to delete a collection or subcollection from your app code, you will have to query for all the documents, then delete them individually.
2023-01-24 23:25:14
How can i do that, im not really sure how? @DougStevenson
2023-01-24 23:25:14
This question has two marked duplicates. Look at those. All you will do are three things that are covered in the documentation and plenty of example code: 1. query the collection 2. iterate teh documents 3. delete each one in that loop.
2023-01-24 23:25:14
@DougStevenson they are all with old syntaxes that don't work with firebase version 9, for example, ref.doc.delete(), doesn't exist anymore, but okey.. :)
2023-01-24 23:25:14
The Firebase documentation explains how to do each one of those three things. If you get stuck on one of them, post a new question with the code you tried that doesn't work the way you expect.
Answers(0) :