I am currently working on a Firebase Cloud Function that is responsible for creating a new Firestore document and then sending back the unique ID of the document to a Flutter application. My functions are written in Typescript.
Below is the code snippet that handles the document creation:
db.collection('devices').doc().set({"deviceId": userId},{merge: true})
.then((docRef: FirebaseFirestore.DocumentReference<FirebaseFirestore.DocumentData>) => {
functions.logger.info(`Document path: ${docRef.path}`,{structuredData: true});
functions.logger.info(`Document written with ID: ${docRef.id}`,{structuredData: true});
response.status(200).send({"result":"success", "docId": docRef.id});
})
.catch((error: Error) => {
functions.logger.error(error,{structuredData: true});
response.status(200).send({"result":"error", "message": error.message});
});
The set
method returns a promise with a payload expected to be of type DocumentReference
containing the id
.
The issue I am facing is that even though the document is successfully written to Firestore
, I am not able to retrieve the values of the DocumentReference
in order to include them in the response.