Search code examples
javascriptfirebasegoogle-cloud-firestoreangularfire

Set custom ID in collection - AngularFire 7 and Firebase


How can i set a custom ID to a doc in the new Angularfire 7?

i DONT want the auto genereted one..

my code: (this creates a automatic ID, a want to put my own ID)

  addInfos(infos: any) {
    const notesRef = collection(this.firestore, "infos");
    return addDoc(notesRef, infos)
  }


Solution

  • You can specify your own document ID and then call setDoc instead of addDoc.

    addInfos(infos: any) {
      const notesRef = collection(this.firestore, "infos");
      return setDoc(doc(notesRef, "your-id"), infos)
    }
    

    This is pretty much a direct copy from the Firebase documentation on setting a document, so I recommend spending some time in there to get up to speed.