-1

I've successfully stored an image in firebase storage but whenever I want to retrieve the download url, it returns Object object. However the log shows valid string url.

P.S have a look at the code below. Thankyou

getImageUrl = async(fileName) => {
    const { currentUser } = firebase.auth();
    const ref = await firebase.storage().ref().child(`${currentUser.uid}/${fileName}.PNG`);
    ref.getDownloadURL()
     .then((url) => {
         console.log('url', url);   //this gives the valid img url
         return url;                //why is it returning the object?
     }).catch(error => {
       console.log('imageError', error);
       return null;
     });
}   


render() {
    return(
      <View style={styles.container}>
        <Text>{(`${this.getImageUrl('2F1560068528')}`)}</Text>  //why is this showing an Object Object instead of download url?
      </View>
    );
}
Amrita Stha
  • 2,973
  • 3
  • 25
  • 46
  • 1
    I could be wrong, but don't async functions return Promise? So you would need to await in render and it should return Promise.resolve(url) or Promise.reject(null) depending ? – Mike Hardy Jun 11 '19 at 16:02
  • Just curious, but what logs out if you do this? ```console.log('url type is', typeof url)``` – SHG21 Jun 11 '19 at 19:03
  • Also, could you try `return JSON.stringify(url)` to see if it stops saying Object object? – kurokiiru Jun 11 '19 at 23:50
  • @kurokiiru I've already tried that it didn't work. – Amrita Stha Jun 12 '19 at 15:15
  • @SHG21 console.log('zzz', typeof url); it gives string and console.log('objectObject', typeof this.getImageUrl('1560068528')); gives object – Amrita Stha Jun 12 '19 at 15:15
  • hmm, try `...return url.json()).then(data => JSON.stringify(data))` then `{this.getImageUrl('2F1560068528')}` – kurokiiru Jun 13 '19 at 10:42

1 Answers1

0

This is why you are getting back an Object. https://firebase.google.com/docs/reference/android/com/google/firebase/storage/FirebaseStorage.html
Firebase.storage returns an Object. It looks like when you are inside ref.getDownloadURL() function, url represents a string, but when it is returned, Firebase converts to an Object.

SHG21
  • 262
  • 2
  • 5