How do I delete specific documents from a FirebaseTS Database with Angular 13?

Viewed 41

Hello I am fairly new to the Angular framework so bear with me please. I have a feed/timeline made out of simple posts that contain text. That text is saved in Firebase. This is how it gets saved:

firestore = new FirebaseTSFirestore();

createItem(postInput: HTMLInputElement) {
    let postMessage = postInput.value;
    let postId = this.firestore.genDocId();

    this.firestore.create(
      {
        path: ["Posts", postId],
        data: {
          postMessage: postMessage,
          timestamp: FirebaseTSApp.getFirestoreTimestamp()
        }
      }
    )
  }

The names of documents inside the Posts folder are randomly generated through let postId = this.firestore.genDocId();. Each document is a post in my timeline. Then I have a "trash bin" icon in my post html component, which I plan on using as a delete button.

<span><i class="fa fa-trash"></i></span>

This is how the list looks like: timeline

What I need is a way to delete the document to which that trash icon belongs to. I have searched the internet but have found little. How do I do it?

Thanks in advance.

1 Answers
  1. You need to add a function click and add the element Id with your icon as:

    <i class="fa fa-trash" (click) = removeItem(id)” >

  2. Then in your TS component you can:

    removeItem(id:any) { this.firestore.db.collection(‘collection name’).doc(id).delete()

Related