javascript - Firebase存储,将图像检索到vue应用

标签 javascript firebase vue.js firebase-storage vuex

我正在尝试从Firebase存储中下载图像以在Vue应用程序中进行渲染,从应用程序到Firebase存储的上传成功,但是在检索到它时出现了错误,我在Vue CLI中使用了Firebase SDK 3设置和vuex来管理我的状态。这是主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 is not a function



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

最佳答案

按照上面的评论,如果我没有误会(未测试...),以下各项应该可以工作。

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

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

关于javascript - Firebase存储,将图像检索到vue应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52242991/

相关文章:

swift - addSnapshotListener 仅获取 firebase 集合中的更改

json - 使用 Typescript 在 vue.js 中加载 JSON 文件

javascript - 为什么我的 Vue 单元测试无法识别函数?

javascript - AngularJS ng-Grid 启动最小化?

javascript - 如何创建每个人都认识邻居的 "grid"布局?

ios - 如何打开应用程序并在 ios 中终止应用程序时显示推送通知

vue.js - VS Code - 在 .vue 文件中启用 autoClosingTags 选项

javascript - 清除与索引分开的输入

javascript - 需要滑动视频的解决方案

ios - 如何从 pod 下载库文件?