【问题标题】:Why forkJoin is returning a wrong value from my observables?为什么 forkJoin 从我的 observables 返回错误的值?
【发布时间】:2020-04-19 07:29:42
【问题描述】:

我有一个应用程序正在使用 Firebase 存储来发送一些图像数据,所以我使用这种方法:

startUpload() {
  if (typeof this.fileList !== 'undefined' && this.fileList.length > 0) {
    const observableList = [];
    for (let i = 0; i < this.fileList.length; i++) {
      // The storage path
      const path = this.userModel.companyCode + `/${new Date().getTime()}_${this.fileList[i].name}`;

      // Totally optional metadata
      const customMetadata = {
        app: 'My AngularFire-powered PWA!'
      };
      const fileRef = this.storage.ref(path);

      // The main task
      this.task = this.storage.upload(path, this.fileList[i], {
        customMetadata
      });

      // Progress monitoring
      this.percentage = this.task.percentageChanges();
      this.snapshot = this.task.snapshotChanges();

      // The file's download URL
      observableList.push(
        this.task.snapshotChanges().pipe(
          finalize(async () => {
            return await fileRef.getDownloadURL();
          }))
      );

      // observableList = this.task.snapshotChanges();
      // observableList.push(taskObservable);
    }
    console.log(observableList);
    forkJoin(
      observableList
    ).subscribe(
      response => {
        console.log(response);
      }
    );
  }
}

这部分:

this.task.snapshotChanges().pipe(
  finalize(async () => {
    return await fileRef.getDownloadURL();
  }))

当我单独使用这个功能并像这样使用时:

this.task.snapshotChanges().pipe(
  finalize(async () => {
    this.downloadUrl = await fileRef.getDownloadURL().toPromise;
  }))

他们返回正确的 URL,this.downloadUrl 是一个全局变量,即 downloadURL: Observable;

但我不想一个一个返回我想要3个结果,所以我有一个想法使用forkJoin,就像javascript中promise的Promise.All():

console.log(observableList);
forkJoin(
  observableList
).subscribe(
  response => {
    console.log(response);
  }
);

我在控制台中得到了这个:

(3) [UploadTaskSnapshot、UploadTaskSnapshot、UploadTaskSnapshot]

我如何从他们那里获得下载 URL?

【问题讨论】:

    标签: javascript angular firebase firebase-storage


    【解决方案1】:

    finalize 接受一个返回类型为 void 的回调函数。这就是为什么它在您的情况下可以单独处理,但在您尝试退货时却不行。 finalize-rxjs

    我认为下面的代码应该适合你

     startUpload() {
        if (typeof this.fileList !== "undefined" && this.fileList.length > 0) {
          const observableList = [];
          const fileRefList = [];
          this.fileList.forEach(file => {
            const path =
              this.userModel.companyCode + `/${new Date().getTime()}_${file.name}`; // Totally optional metadata
            const customMetadata = { app: "My AngularFire-powered PWA!" };
            fileRefList.push(this.storage.ref(path)); // The main task
            this.task = this.storage.upload(path, file, {
              customMetadata
            }); // Progress monitoring
            this.percentage = this.task.percentageChanges();
            this.snapshot = this.task.snapshotChanges();
            observableList.push(this.snapshot);
          });
          console.log(observableList);
          forkJoin(observableList)
            .pipe(map(async (_, i) => await fileRefList[i].getDownloadURL()))
            .subscribe(response => console.log(response));
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-08
      • 1970-01-01
      相关资源
      最近更新 更多