Search code examples
angular5google-cloud-firestoreangularfire2

How to retrieve id of just inserted firestore document (with angularfire2)?


I'm inserting a new document into my firestore collection like so:

this.afs.collection<Client>('clients').add(this.form.value).then(docRef => {
  console.log("Need to output the firestore generated doc id of the document here: ...")
})

Solution

  • Got it: then() takes a union type (http://www.typescriptlang.org/docs/handbook/advanced-types.html): void | DocumentReference.

    Therefore needs to be addressed like this:

    this.afs.collection<Client>('clients').add(this.form.value).then(docRef => {
      console.log((docRef) ? (<DocumentReference>docRef).id : 'void') // docRef of type void | DocumentReference
    })