【问题标题】:Firebase storage getDownloadURL creates infinite loopFirebase 存储 getDownloadURL 创建无限循环
【发布时间】:2019-03-21 21:05:32
【问题描述】:

当尝试从 Firebase 存储上传的文件中检索 URL 时,我遇到了一个无限循环,它锁定了我的浏览器。

HTML

<div class="row" v-for="(item, idx) in data" :key="item.id">
  <img :src="get_image_url(item.id, item.images)">
</div>

VueJS 函数 获取图片

get_image_url: function(id, images) {
  storage.ref().child('products/' + id + '/' + images[0]).getDownloadURL()
  .then(url => {
    console.log(url)
    return url
  })
  .catch(err => {
    console.log(err)
  })
}

获取数据

created: function() {
  db.collection('shops').doc(vm.$route.params.id)
  .collection('inventory').get()
  .then(res => {
    res.forEach(doc => {
      const id = doc.id
      const data = doc.data()
      vm.data.push({ id, ...data })
    })
  })
}

【问题讨论】:

  • 我对vue不熟悉,但是错误信息看起来和你展示的代码无关
  • 能否请您添加什么是数据,如何获取和存储它。
  • 我能够通过在直接下载数据时获取图像来使其正常工作(请参阅更新)。

标签: vuejs2 firebase-storage infinite-loop nuxt.js


【解决方案1】:

好像在获取数据的同时获取图片的url不会导致死循环。

HTML

<div class="row" v-for="(item, idx) in data" :key="item.id">
  <img :src="item.url">
</div>

Vue 创建

created: function() {
  db.collection('shops').doc(vm.$route.params.id)
  .collection('inventory').get()
  .then(res => {
    res.forEach(doc => {
    storage.ref().child('products/' + doc.id + '/' + doc.data().images[0]).getDownloadURL()
    .then(resp => {
      const id = doc.id
      const data = doc.data()
      const url = resp
      vm.data.push({ id, resp, ...data })
    })
    .catch(err => {
      console.log(err)
    })
  })
}

【讨论】:

  • 我在 Vue.js 中做类似的事情时也遇到了这个问题。但是,我发现根据 stackoverflow.com/questions/53055190/… 中的 Firebase 团队的说法,该 URL 似乎是要保留的。 “客户每次都打电话给 [getDownloadURL()] 是浪费时间。”
猜你喜欢
  • 1970-01-01
  • 2021-04-06
  • 2018-11-12
  • 2019-09-30
  • 1970-01-01
  • 2021-02-20
相关资源
最近更新 更多