When the user update your profile picture I need to upload his photo to Firebase Storage and immediately retrieve the download URL to update his profile collection.
In another scenario, when the user update his profile, he do not change his profile picture, so I don't have the imageData and also don't need to upload his picture, just update his profile information.
My saveProfile() method:
if (this.profileForm.valid) {
....
//Check if the user change his picture. If yes, the userProfilePicture has data.
If (userProfilePicture) {
//upload the picture to firestorage
const path = `/profile/${this.profile.id}.jpg`;
const task = this.afStorage
.ref(path)
.putString(imageData, 'base64', { contentType: 'image/jpeg' });
// subscribe to download url
task.downloadURL().subscribe(
url => {
//updating profileUrl in the form
this.profileForm.patchValue({ avatar: url });
//Doing the update in Firestore
this.afs.doc(`/profile/${id}`).update(this.profileForm.value);
});
}
Else {
// just update the profile information
this.afs.doc(`/profile/${id}`).update(this.profileForm.value);
}
}
I want to avoid to duplicate the update code in 2 places. Is there a more convenient way tho achieve this? Maybe if there is a way to do the update just when I have the downloadUrl available (in 2 scenarios) something like this:
If (userProfilePicture) {
//upload the picture
//subscribe to get the download url
}
//AWAIT for the subscribe to return the download URL and when the download URL is available then update
?? How to do this
//update the profile information with the new or existent download URL
await??? this.afs.doc(`/profile/${id}`).update(this.profileForm.value);
downloadURLis depreciated, you might need to switch togetDownloadURLon the.ref(path).awaitneeds a promise. I think thatputStringreturns an upload task that if you callonorthenyou can get a promise back.getDownloadURLalso returns a promise. So perhaps inside theif(profilePicture)block you can await on the upload task and then await on the getDownLoadURL and then instead of using theElseblock just always callupdate(this.profileForm.value)- James Poag