简体   繁体   English

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

[英]firebase.storage().ref().getDownloadURL() won't run

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.我正在尝试将图像上传到 Firebase 存储并将 downloadURL 保存在 Firestore 中,但是当我的代码运行时,.getDownloadURL() 不会运行。 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)
    })
  })
})

You're not getting any results or logs after console.log('Img ') because you're not chaining the promise properly.console.log('Img ')之后您没有得到任何结果或日志,因为您没有正确链接 promise。 You must call the getDownloadURL() before chaining .then() .您必须在链接getDownloadURL() .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 .有关更多信息,您可以查看此文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:firebase.storage 不是 function - TypeError: firebase.storage is not a function Firebase 存储 listAll() 和 getDownloadURL() 不起作用 - Firebase Storage listAll() together with getDownloadURL() doesn't work out 如何同时运行 getDownloadURL 和 getMetaData 并将结果输入同一个 object? Firebase 存储 - How do I run both getDownloadURL and getMetaData at once and get the results into the same object? Firebase Storage 您试图通过调用 firebase.storage() 来使用未安装在 android 项目上的 firebase 模块 - You attempted to use a firebase module that's not installed on your android project by calling firebase.storage() AngularFire2 - Firebase 存储 getDownloadURL() - 如何返回 firestore 的 url - AngularFire2 - Firebase storage getDownloadURL() - How to return the url for firestore 如何在 Angular2 服务中注入 FirebaseApp 以将 firebase.storage() 与 AngularFire2 一起使用 - How to inject FirebaseApp in Angular2 service to use firebase.storage() with AngularFire2 Flutter Web - 图片不会上传到 Firebase 存储 - Flutter Web - Image won't upload to Firebase Storage Firebase:Error.getDownloadURL 不是 function - Firebase: Error .getDownloadURL is not a function ref 不是 function,firebase 存储图片上传,reactJS - ref is not a function, firebase storage image upload, reactJS 当我上传图像 firebase 存储 getdownloadUrl() 将下载 url 放入 Firestore Collections 但未在 React JS 中设置 - When I upload a image firebase storage getdownloadUrl() put download url In Firestore Collections but not set it in React JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM