Home > Software design >  Set custom ID in collection - AngularFire 7 and Firebase
Set custom ID in collection - AngularFire 7 and Firebase

Time:02-01

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)
  }

CodePudding user response:

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.

  •  Tags:  
  • Related