【问题标题】:Problem with async functions with Firebase Storage and Firestore on Ionic3Ionic3 上的 Firebase 存储和 Firestore 的异步功能问题
【发布时间】:2019-05-05 08:25:16
【问题描述】:

我正在尝试从我上传的图片中获取下载 URL。 使用此功能,我将图像上传到 Firebase 存储

uploadImage() {
  this.image = 'movie-' + new Date().getTime() + '.jpg';
  let storageRef: any,
  parseUpload: any;
  return new Promise((resolve, reject) => {
  storageRef = firebase.storage().ref('imgs/' + this.image);
  parseUpload = storageRef.putString(this.cameraImage, 'data_url');     
})

问题是我想保存图像 URL,以便我可以从 Firestore 上的文档中获取它。 为了做到这一点,我有这个函数可以在图片上传后获取 dowloadURL。

如果我没有错,这个方法应该返回一个字符串,如果我错了,请纠正我,因为如果不是,我将无法将它保存在 Firestore 上

async getImgFromServer(imgName: string) {
  let img;
  let downloadIMG;
  img = firebase.storage().ref("/imgs/" + imgName).getDownloadURL();
  let ref= firebase.storage().ref();
  const imgRef = ref.child("/imgs/" + imgName);
  const downloadURL = await imgRef.getDownloadURL()
  return downloadURL
}

唯一的问题是,为了这样做,必须上传图片。 我知道如果上传图片,这个函数总是会返回 url。 问题是,当 uploadImage() 完成时,我不知道如何使此功能工作。 获取 url 后,这是我将 url 保存在 Firestore Cloud 数据库中的功能。

 updateImgFromServer(image,id){
  this.firestore.doc(`public/${id}`).set({
    img:image,
  });
 }

有没有办法确保第二个函数(getImgFromServer()) 仅在第一个函数(uploadImage()) 完成后执行? p>

我知道一旦文件上传到我放的第一个 sn-p 上,就有办法获取 downloadURL。如果有人知道如何使它工作,那也很好。

【问题讨论】:

    标签: typescript firebase ionic-framework ionic3 firebase-storage


    【解决方案1】:

    putString() 是异步的,并返回一个跟踪上传进度的UploadTask,您需要使用它。它有一个then() 方法,就像一个承诺一样工作,所以用它来确定上传何时完成。

    【讨论】:

    • 我已经关注了 UploadTask 文档以及这个 [one] (firebase.google.com/docs/storage/web/upload-files?hl=es-419)。但是,当我尝试上传它时,我收到错误消息:ERROR TypeError: Cannot read property 'firestore' of undefined。这是我目前的代码,我将上传image,因为代码不适合这里。希望你能帮助我。
    • 这听起来与您最初提出的问题不同。请用您的详细信息提出一个单独的问题
    • 完成,感谢我对 StackOverflow 有点陌生,并且不知道何时创建新问题的限制。这是link,以防你能帮助我。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 2019-07-22
    相关资源
    最近更新 更多