firebase.storage().ref().getDownloadURL() 不会运行

发布于 2025-01-18 04:37:41 字数 664 浏览 1 评论 0原文

我正在尝试将图像上传到Firebase存储中,并将下载图保存在Firestore中,但是当我的代码运行时,.getDownloadurl()将无法运行。图像上传,但在那之后,什么都没有发生。我的代码在下面。我尝试了几张图像。

const task = firebase.storage().ref('/hendingar/' + img.name).put(img)

task.then((snapshot) => {
  console.log('Img ')
  snapshot.ref.getDownloadURL((url) => {
    console.log('Url')
    firebase.firestore().collection('hendingar').add({
      title: title,
      description: description,
      start: start,
      end: end,
      place: infoPlace,
      prices: temp_arr,
      img: url,
    }).then(() => {
      console.log('Success')
    }).catch((error) => {
      console.error(error)
    })
  })
})

I am trying to upload an image to Firebase Storage and save the downloadURL in Firestore, but when my code is running, .getDownloadURL() won't run. The image uploads, but after that, nothing happens. My code is below. I've tried with several images.

const task = firebase.storage().ref('/hendingar/' + img.name).put(img)

task.then((snapshot) => {
  console.log('Img ')
  snapshot.ref.getDownloadURL((url) => {
    console.log('Url')
    firebase.firestore().collection('hendingar').add({
      title: title,
      description: description,
      start: start,
      end: end,
      place: infoPlace,
      prices: temp_arr,
      img: url,
    }).then(() => {
      console.log('Success')
    }).catch((error) => {
      console.error(error)
    })
  })
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧梦荧光笔 2025-01-25 04:37:41

console.log('Img ') 之后您没有收到任何结果或日志,因为您没有正确链接承诺。您必须在链接 .then() 之前调用 getDownloadURL()。请参阅下面的代码:

const task = firebase.storage().ref('/hendingar/' + img.name).put(img)

// Uploads the file to Firebase Storage.
task.then((snapshot) => {
  console.log('Img')
  // You can also put the creation of Firestore Document here.
  // (If you want it to create the document before getting the download URL.)
  
 // Calls the `getDownloadURL()` method.
 snapshot.ref.getDownloadURL()
  .then((url) => {
    // Returns the Download URL from Firebase Storage.
    console.log('Url')
    console.log(url)
    
    // You can put this either here or before getting download URL.
    firebase.firestore().collection('hendingar').add({
      title: title,
      description: description,
      start: start,
      end: end,
      place: infoPlace,
      prices: temp_arr,
      img: url,
    }).then(() => {
      console.log('Success')
    }).catch((error) => {
      console.error(error)
    })
  })
})
.catch((error) => {
  // Logs error if there's an error uploading of file.
  console.log(error)
})

我对上面的代码留下了一些注释以便更好地理解。
如需了解更多信息,您可以查看此文档

You're not getting any results or logs after console.log('Img ') because you're not chaining the promise properly. You must call the getDownloadURL() before chaining .then(). See code below:

const task = firebase.storage().ref('/hendingar/' + img.name).put(img)

// Uploads the file to Firebase Storage.
task.then((snapshot) => {
  console.log('Img')
  // You can also put the creation of Firestore Document here.
  // (If you want it to create the document before getting the download URL.)
  
 // Calls the `getDownloadURL()` method.
 snapshot.ref.getDownloadURL()
  .then((url) => {
    // Returns the Download URL from Firebase Storage.
    console.log('Url')
    console.log(url)
    
    // You can put this either here or before getting download URL.
    firebase.firestore().collection('hendingar').add({
      title: title,
      description: description,
      start: start,
      end: end,
      place: infoPlace,
      prices: temp_arr,
      img: url,
    }).then(() => {
      console.log('Success')
    }).catch((error) => {
      console.error(error)
    })
  })
})
.catch((error) => {
  // Logs error if there's an error uploading of file.
  console.log(error)
})

I leave some comments on the code above for better understanding.
For more information, you may checkout this documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文