0
votes

How do I delete a specific document within a collection on the firestore using react?

I looked here and on google but I didn't find anything intrinsically related.

I'm just managing to delete the document with index [0] from my collection.

this.state.idDoc recives all id documents.

If I just pass the .doc(idDoc) I get the following error:

FirebaseError: Function CollectionReference.doc() requires its first argument to be of type non-empty string, but it was: an array

What am I doing wrong? any help is welcome.

constructor(props) {
    super(props);
    this.state = {
     idDoc: = [],
    }

delete = () => {
      let idDoc = this.state.idDoc;
      firebase.firestore().collection("newPost").doc(idDoc[0]).delete()
      .then(function() {
        console.log("Document successfully deleted!");
      }).catch(function(error) {
          console.error("Error removing document: ", error);
      });
    }

html button

<div>
     <button id="PostDelete" onClick={this.delete}> <ion-icon name="ellipsis-horizontal-sharp"> </ion-icon> </button> 
</div>
1
You have to pass the id of the document to deletepepe
I do this, but I can only get index [0] from the array where my document ids are, so I can only delete index 0 from the array. when I have many posts within that collection I will not be able to delete others except the index document [0]. I wanted to learn how to do it in a dynamic way, without having to create several functions to delete each index of the array.Joao Paulo Gardiano
The error says you are passing an array instead of string,to delete each document you need to iterate over the arraypepe
I'm still having the same problem. I can get all the document ids but I can't find a way for my post deletion function to identify which post is being deleted specifically. every time I run my function, it only deletes the first document created in the collection.Joao Paulo Gardiano
already tested to return the ids to a string, but still deleting only the first document created, I can't find a way to recognize which document is being deleted by the id. if i remove the index [0] from idDoc, i get an error from firebase saying that they only accept arguments in .doc () of type stringJoao Paulo Gardiano

1 Answers

0
votes

The id you are passing to the collection is in a invalid format.