Using a value as the name for the collection in firebase instead of a hardcoded path when creating a collection and adding a document

Issue

Im trying to add a document inside of a new collection with the addDoc function.

 async addSongtoFire(description: string) {
    await addDoc(collection(this.db, this.Event), {
      Name: description,
      like: false,
      likes: 0
    })
  }

I want the name of the collection to be created to the value of this.Event. The Problem is that only Strings like "Name" are accepted. I tried to solve the problem with ['${this.Event}'] but this also didnt worked. The following error code occurs:

No overload matches this call.
The last overload gave the following error.
Argument of type ‘Firestore’ is not assignable to parameter of type ‘DocumentReference’.
Type ‘Firestore’ is missing the following properties from type ‘DocumentReference’: converter, firestore, id, path, and 2 more.ts(2769)

How can i convert the path to be the value of this.Event without using a hardcoded path?

Solution

I fixed the problem myself; I just needed to use the toString() method:

async addSongtoFire(description: string) {
    await addDoc(collection(this.db, this.Event.toString()), {
      Name: description,
      like: false,
      likes: 0
    })
  }

Answered By – Rashorr

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published