Jean-Marie Dalmasso
Jean-Marie Dalmasso

Reputation: 1157

Unable to retrieve Firebase downloadURL from .getDownloadURL() promise

I am trying to retrieve the downloadURL from an image I uploaded to Firebase. No problem getting the correct URL inside the promise.

I don't understand why downloadURL is still undefined after I set it equal to the url returned in the .then statement.

let downloadURL;

    firebase
      .storage()
      .ref()
      .child('project_images/' + imageName)
      .getDownloadURL()
      .then(url => {
        downloadURL = url;
        console.log(downloadURL); //logs the correct downloadURL needed
      })
      .catch(error => {});

    console.log(downloadURL); //still undefined

I feel like I am missing something stupid but for the life of me I can't see what it is. Any help appreciated. Thank you!

Upvotes: 0

Views: 413

Answers (1)

Jordan
Jordan

Reputation: 491

Does it log the undefined before the one inside the promise?

It's likely just not completing the promise before you try and log outside the function calls.

Upvotes: 1

Related Questions