代码之家  ›  专栏  ›  技术社区  ›  bellotas

从可观察的<string>

  •  0
  • bellotas  · 技术社区  · 6 年前

    我有一个 Observable<string> 打电话 this.downloadURL 调用“我的数据库”后,将获得以下值:

    enter image description here

    我希望通过以下方法获得其值:

     //Upload a new picture to the database as a profile picture
          uploadBetPicture(event) {
            const id =
              "bet_" +
              Math.random()
                .toString(36)
                .substring(2) +
              "_Photo";
            this.ref = this.afStorage.ref(id);
            this.task = this.ref.put(event.target.files[0]);
            this.downloadURL = this.task.downloadURL();
            //UNTIL HERE I HAVE JUST STORE THE PICTURE AND SET THE OBSERVABLE
    
            //HERE I GET THE ERROR
            this.photoUrl = this.downloadURL.value;
          }
    

    尽管如此,即使我在控制台中看到该值,我也无法访问该变量,并且在 this.photoUrl = this.downloadURL.value

    类型“Observable”上不存在属性“value”。

    只需补充一点,存储方法是有效的,正如我前面所说的,我得到 value

    2 回复  |  直到 6 年前
        1
  •  2
  •   SiddAjmera    6 年前

    自从 this.task.downloadURL() 是一个可观察的和你的 downloadURL 被包裹在里面的某个地方 Observable ,你必须 subscribe

    试试这个:

    uploadBetPicture(event) {
      const id =
        "bet_" +
        Math.random()
        .toString(36)
        .substring(2) +
        "_Photo";
      this.ref = this.afStorage.ref(id);
      this.task = this.ref.put(event.target.files[0]);
      this.task.downloadURL()
        .subscribe(value => this.photoUrl = value);
    }
    

    注:

    value 内部 订阅 封锁 console 然后使用适当的 key 对象

        2
  •  1
  •   code4life    6 年前

    查阅RxJs文档,您将看到角度形式的reactive非常有用,因为您所做的不仅仅是订阅和等待异步回调。

    YourObservable.first()

    http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-first



    或最后一个值:

    YourObservable.last()

    http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-last


    完整的可观察文件如下:

    http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html