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: ...")
})
Try
this.afs.collection<Client>('clients').add(this.form.value).then(docRef => {
console.log(docRef.id);
})
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
})
You can generate a key by yourself.
...
this.generatedKey = this.generateNewKey('clients');
...
this.afs.collection<Client>(`clients`).doc(this.generatedKey)
.set({ someField: 'some value' })
.then(docRef => {
console.log("Self generated doc ID: ...", this.generatedKey )
})
generateNewKey(ref: any) {
const _ref = firebase.firestore().collection(ref).doc();
const newKey = _ref.id;
return newKey;
}