Home > Mobile >  ionic Angular 9 wait until file download url is available
ionic Angular 9 wait until file download url is available

Time:02-06

I am using ionic 5 and angular 9 to upload a data url to firebase storage. My code works good but i am not sure how to wait until the download url is avaialble.

below is the code

uploadToFireStore(imageData){
    const storage = getStorage();
    const storageRef = ref(storage, (new Date().getTime().toString()));
    uploadString(storageRef, imageData, 'data_url').then((snapshot) => {
      getDownloadURL(snapshot.ref).then((downloadURL) => {
        console.log('File available at', downloadURL);
        this.downloadURL = downloadURL
      });
    });
  }

I am calling it in my camera plugin like this

this.uploadToFireStore(base64Image)
console.log("updated with url:", this.downloadURL)

currently the download url is printed later and my code is not waiting for download url to be there. I need this url to upload to my RTDB so please advise how can i do it without moving the code into this part

getDownloadURL(snapshot.ref).then((downloadURL) => {
...

CodePudding user response:

You probably just need to return your promise chain:

uploadToFireStore(imageData){
    const storage = getStorage();
    const storageRef = ref(storage, (new Date().getTime().toString()));
    return uploadString(storageRef, imageData, 'data_url').then((snapshot) => {
      return getDownloadURL(snapshot.ref).then((downloadURL) => {
        console.log('File available at', downloadURL);
        this.downloadURL = downloadURL
      });
    });
  }

Then you can do this:

this.uploadToFireStore(base64Image).then(() => {
   console.log("updated with url:", this.downloadURL)
});
  •  Tags:  
  • Related