等待可观察的完成-Angular

时间:2018-10-21 17:31:43

标签: angular firebase rxjs google-cloud-firestore observable

我正在使用Angular和Firebase存储上载图像,当我尝试在urlImg的{​​{1}}中打印它时,我的undefinedconsole.log的问题tap

 this.snapshot = this.snapshot = this.task.snapshotChanges().pipe(
      finalize(() => {
        this.storage
          .ref(path)
          .getDownloadURL()
          .subscribe(url => {
            this.urlImg = url; // with this you can use it in the html
          });
      }),
      tap(snap => {
        if (snap.bytesTransferred === snap.totalBytes) {
          // Update firestore on completion
          //Code to upload on completion
          console.log("url");
          console.log(this.urlImg);
        }
      })
    );

1 个答案:

答案 0 :(得分:0)

一种选择是利用已完成的订阅部分。然后,您可以确保this.urlImg具有值。

.subscribe(
   url => this.urlImg = url,
   err => console.error('Request for URL failed: ' + err),
   () => {
      // operations when URL request is completed
   }
)