firebase存储,将图像检索到vue应用程序

问题描述 投票:0回答:1

我正在尝试从我的firebase存储中下载图像以在我的Vue应用程序中呈现它,从应用程序到firebase存储的上传成功,但是在检索时它给我一个错误,我在Vue CLI中使用firebase SDK 3 setup和vuex来管理我的状态。这是我在main store.js文件中的操作中的函数设置

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))
  
  
  
  
  
  
  
}

我得到的错误是:

TypeError:imageInfo.getDownloadURL不是函数

我再次相信问题出在then()块中,我从firebase存储中检索图像。提前致谢

javascript firebase vue.js firebase-storage vuex
1个回答
3
投票

根据上述评论,如果我没有误会(未经测试......),以下内容应该有效。

请注意,getDownloadURL()返回一个承诺(请参阅here),因此您必须链接承诺。

  ....
  .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
      })
  })
  ....
© www.soinside.com 2019 - 2024. All rights reserved.