Reputation: 107
Regarding Firestore, Cloud Function, Typescript.
My goal:
Problem: A new doc is created, however, it uses the auto-gen Doc ID and the doc itself has an additional Doc ID field within.
Note: Using the Firebase Website, I can manually add new documents to a collection using my own Doc ID.
Example Code :
export const onNewRegistration = functions.auth.user().onCreate((user) => {
functions.logger.info("New User Created - Name: ", user.email, ", UID: ", user.uid);
const db = admin.firestore();
const newdoc = db.collection('UserAccounts').add({
'Document ID': user.uid,
State: 'Unverified'
});
return newdoc;
});
Thank you, all.
Upvotes: 2
Views: 1590
Reputation: 317372
If you know the ID of the document to create, don't use add()
. add()
always creates a new document with a new random ID.
You should instead use doc()
to create a new document reference with the given ID, then use set()
to write it:
return db.collection('UserAccounts').doc(user.uid).set({
// fields and values here will be written to the document
});
See also: Firestore: difference between set() and add()
Upvotes: 5