Delete item from a subcollection in firebase with react

Viewed 48

I made the subcollection 'medicine" from the collection 'users', im trying to delete each item from that subcollection when clicking a button, this is what i did:

import {auth, db} from "../../firebase/firebase.config";
import {doc, deleteDoc} from "firebase/firestore";

function BotonEliminar(id) {

    const user = auth?.currentUser?.uid;

    const deleteElement = async () =>{
       await deleteDoc(doc(db, 'users', user, 'medicine', id));
        
    }

.... etc

I got in the console:

Uncaught (in promise) TypeError: n.indexOf is not a function

I don't know what I doing wrong. I get the id and the user correctly.


Doing the console.log(db, user, id);

enter image description here

How the database looks like:

enter image description here

1 Answers

The issue appears to be that id is an Object, not a String. (From the console screenshot, it looks like {id: 'liKQ1I0RjliCQVKFHvc'} rather than just 'liKQ1I0RjliCQVKFHvc')

Assuming that BotonEliminar is meant to be a React component, the argument is a React props object rather than the actual id. You probably want to change the function signature to destructure the id from the props:

function BotonEliminar({id}) { // note the { and }

and then use id normally.

Related