Before sending a group of writes to Firestore with an automatically generated ID, I have a function that uploads an image file to Firebase storage. The code is set up to wait for the file to be uploaded before getting the download URL and then uploading the documents to Firestore.
However, I would like to use the Firestore document's auto-generated ID as the storage reference.
Below is the code snippet:
try {
// Replace "organization.uid" with the auto-generated ID by Firestore "organization_doc_ref.id".
const storageRef = ref(this.storage, `organizations/${organization.uid}`);
await uploadBytes(storageRef, blob).then(() => {
console.info('Uploaded a blob or file!');
});
organization.logo_url = await getDownloadURL(storageRef);
const batch = writeBatch(this.firestore);
const organization_doc_ref = doc(
collection(this.firestore, 'organizations')
);
batch.set(organization_doc_ref, organization);
const usernameDocRef = doc(
this.firestore,
'usernames',
organization.username
);
batch.set(usernameDocRef, { uid: organization_doc_ref.id });
await batch.commit();
console.log('Organization successfully created...');
} catch (error) {
return console.error('Error writing document: ', error);
}