Attempting to retrieve the downloadURL from an uploaded image. The uploadImage function is used to upload the image to Firebase Storage.
uploadImage() {
this.image = 'movie-' + new Date().getTime() + '.jpg';
let storageRef: any,
parseUpload: any;
return new Promise((resolve, reject) => {
storageRef = firebase.storage().ref('imgs/' + this.image);
parseUpload = storageRef.putString(this.cameraImage, 'data_url');
})
The goal is to save the image URL for retrieval from a Firestore document. This function is used to obtain the downloadURL after the image is uploaded.
If my understanding is correct, this method should return a string, as it needs to be saved in Firestore.
async getImgFromServer(imgName: string) {
let img;
let downloadIMG;
img = firebase.storage().ref("/imgs/" + imgName).getDownloadURL();
let ref= firebase.storage().ref();
const imgRef = ref.child("/imgs/" + imgName);
const downloadURL = await imgRef.getDownloadURL()
return downloadURL
}
The challenge is ensuring that the second function (getImgFromServer()) executes only after the first one (uploadImage()) is completed. Once the URL is obtained, it is saved in the Firestore Cloud database using the updateImgFromServer function.
updateImgFromServer(image,id){
this.firestore.doc(`public/${id}`).set({
img:image,
});
}
If anyone has a solution to efficiently synchronize the upload and retrieval of image URLs, please share. It would greatly help in optimizing the process.