从订阅中检索文件路径引用并将其存储到Firebase

时间:2019-10-11 06:19:55

标签: angular typescript firebase rxjs firebase-storage

我正在运行此脚本以在提交表单后上传我的图片

updateUser(user: User, privateUser: PrivateUser, dob) {
  //store img in storage
  if(this.file){
  var path = `users/${this.userID}/${this.file.name}`
  var ref = this.storage.ref(path);
  this.storage.upload(path, this.file);

    this.downloadURL = ref.getDownloadURL();
    this.refURL =this.downloadURL.subscribe(url => console.log(url) );
    console.log(this.refURL);

  }
}

,并且我试图将下载URL存储到我的Firebase中作为参考点。 此时,控制台正在从此行打印出我想要的内容 this.refURL =this.downloadURL.subscribe(url => console.log(url) ); 但是console.log(this.refURL);却给我返回了一个订阅者。 我应该怎么做才能将url值存储到firebase中?

1 个答案:

答案 0 :(得分:1)

订阅可观察对象将返回订阅,这就是您订阅可观察对象的方式,以监听变化并获取值,从而执行操作

this.downloadURL.subscribe(url => {
this.refURL=url;//or some other action here
})

或转换为promiseawait以获得响应

async updateUser(user: User, privateUser: PrivateUser, dob) {
  //store img in storage
  if(this.file){
  var path = `users/${this.userID}/${this.file.name}`
  var ref = this.storage.ref(path);
  this.storage.upload(path, this.file);

    this.downloadURL = ref.getDownloadURL();
    this.refURL= await this.downloadURL.toPromise();
    console.log(this.refURL);

  }
}

demo