firebase storage, retrieving an image to a vue app

Viewed 4069

I am trying to download an image from my firebase storage to render it in my Vue app, the upload from the application to the firebase storage is successful, however upon retrieval it gives me an error, i am using the firebase SDK in a Vue CLI 3 setup and vuex to manage my state. Here is the function setting in my actions in the main store.js file

createMeetUp({commit, getters}, payload) {
  //here my payload is an object contains the following props
  const meetup = {
    title: payload.title,
    location: payload.location,
    date: payload.date.toISOString(),
    description: payload.description,
    creatorId: getters.user.id
  }
  
  let imageUrl
  let key
  
  //now i am reaching out to the firebase database to store the above object
  firebase.database().ref('meetup').push(meetup)
  .then(data => {
    key = data.key
    return key
  })
  .then(key => {
    //also in my payload object i stored an image file
    //so here i am uploading the image to the firebase storage
    const fileName = payload.image.name
    const extension = fileName.slice(fileName.lastIndexOf('.'))
    return firebase.storage().ref('meetup/' + key + '.' +       extension).put(payload.image)
  })
  .then(imageInfo => {
  //the issue is here in this then() block as i am stuck on how to retrieve the image from the storage to render it in the app
    imageUrl = imageInfo.getDownloadURL()
    return firebase.database().ref('meetups').child(key).update({
          imageUrl: imageUrl
      })
  })
  .then(() => {
    //here i am simply commiting my mutation.. 
    commit('createMeetUp', {
      ...meetup,
      imageUrl: imageUrl,
      id : key
    })
  })
  .catch(err => console.log(err))
  
  
  
  
  
  
  
}

the error I am getting is:

TypeError: imageInfo.getDownloadURL is not a function

Again I believe the issue is in the then() block where I retrieve the image from the firebase storage. thanks in advance

1 Answers

Following the comments above, the following should work if I am not mistaking (not tested...).

Note that getDownloadURL() returns a promise (see here), therefore you have to chain the promises.

  ....
  .then(key => {
     //also in my payload object i stored an image file
     //so here i am uploading the image to the firebase storage
     const fileName = payload.image.name
     const extension = fileName.slice(fileName.lastIndexOf('.'))
     return firebase.storage().ref('meetup/' + key + '.' + extension).put(payload.image)
  })
  .then(uploadTaskSnapshot => {
     return uploadTaskSnapshot.ref.getDownloadURL()
  })
  .then(imageUrl => {
    return firebase.database().ref('meetups').child(key).update({
          imageUrl: imageUrl
      })
  })
  ....
Related