简体   繁体   中英

Firestore: Delete document from a collection in firestore

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM