【问题标题】:Applying images to app from FireStore Storage将图像从 FireStore 存储应用到应用程序
【发布时间】:2020-10-02 11:45:21
【问题描述】:

所以我能够成功地将图像上传到 FireStore 存储。

现在我正在尝试将存储在 FireStore 中的图像应用到应用程序上的用户配置文件中。

import {ActivatedRoute} from '@angular/router';
import { AngularFireStorage } from 'angularfire2/storage';

export class ProfileComponent implements OnInit {

  routeParams;
  profilePicture: any = "../../assets/default-img.png"

  constructor(
  private afStorage: AngularFireStorage,
  private route: ActivatedRoute,
  ) { }


  ngOnInit() 
  {
    this.routeParams = this.route.params.subscribe(
      params => {
          /*Update Variables here*/
          if (email.__email != "")
          {
            let path = "/" + email.__email + "/profile.png";
            this.profilePicture = this.afStorage.ref(path).getDownloadURL();
          }
      });
  }
}

我要做的就是从 FireStore 存储中检索 img 并将其应用到一个人的个人资料图片。

【问题讨论】:

    标签: angular typescript firebase google-cloud-firestore


    【解决方案1】:

    查看文档here

    'getDownloadURL()' 返回一个 Promise,因此您必须解析该 Promise 才能真正从 URL 获取数据。如果您的 HTML 模板使用的是profilePicture,您可以在ngOnInit() 中将 URL 设置为图像源:

    this.afStorage.ref(path).getDownloadURL().then(function(url) {
      var img = document.getElementById('myimg');
      img.src = url;
    }).catch(function(error) {
      // Handle any errors
    });
    

    【讨论】:

    • 我运行代码并返回错误。首先它说“Observable 类型中不存在属性'then'”。然后我在 getDownloadURL() 和 .then 之间添加 '.toPromise()' 以消除此错误。但是代码仍然不起作用,因为 catch 错误返回 [object object] 作为错误
    猜你喜欢
    • 2016-09-23
    • 1970-01-01
    • 2016-11-20
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 2020-06-01
    • 1970-01-01
    相关资源
    最近更新 更多