I am using firebase, anguarfire2 and i want when i create a new document in a collection, the generated id to be saved also in a field called id? Is that possible?
Asked
Active
Viewed 48 times
0
-
Check [this](https://stackoverflow.com/a/56981537/5246885) out. – Alex Mamo Nov 01 '19 at 14:39
-
I was thinking about a solution. Call the function to create the document with the fields i want and then get a snapshot to the current document in order to get the id. Then use set method to update the document. What do you think? @AlexMamo – Man H Nov 01 '19 at 14:42
-
Check also [this](https://stackoverflow.com/questions/51470538/angular-firebase-firestore-get-document-id) out. – Alex Mamo Nov 01 '19 at 14:48
1 Answers
0
The approach and syntax for the Firebase Javascript library is as below. It should almost same for AngularFire2.
const docRef = await firestore()
.collection("some-collecton")
.doc();
await docRef.set({ id: docRef.id, ...otherDocumentData });

Sanket Patel
- 541
- 6
- 12