简体   繁体   English

Angular 中可观察到的 Promise 7+

[英]Promise inside Observable in Angular 7+

I have a problem that I can't solve.我有一个我无法解决的问题。

In the ngOnInit event I observe the url parameter.ngOnInit事件中,我观察到 url 参数。 This parameter corresponds to a folder in firebase-storage .此参数对应于firebase-storage中的文件夹。 That way when loading I get a list of folders and/or files inside that folder that is being informed and storing it inside listReferences variable which is of type Reference[].这样,在加载时,我会在该文件夹中获得一个文件夹和/或文件列表,该列表被通知并将其存储在类型为 Reference [] 的listReferences变量中。

Here is the code:这是代码:

ngOnInit() {
  this.route.params
    .subscribe(params => {
      this.getFiles(params.ref).subscribe(
        (listReferences) => {
          this.listReferences = listReferences;
        }
      );
    }
  );
}

getFiles(folder: string) {
  return this.storage.ref('/' + folder).listAll()
  .pipe(
    map((data) => {
      return data.items;
    })
  );
}

It turns out that for each item in the listReferences array I need to access the getDownloadUrl() or getMetadata() method which are promising and I am unable to retrieve the values for each item in the array.事实证明,对于listReferences数组中的每个项目,我需要访问有希望的getDownloadUrl()getMetadata()方法,并且我无法检索数组中每个项目的值。 How should I proceed in this case?在这种情况下我应该如何处理? How best to do this?如何最好地做到这一点?

Basically I am following the information contained in the reference guide.基本上我遵循参考指南中包含的信息。 https://firebase.google.com/docs/reference/js/firebase.storage.Reference https://firebase.google.com/docs/reference/js/firebase.storage.Reference

How about using ForkJoin as follows:如何使用ForkJoin如下:

ngOnInit() {
  this.route.params
    .pipe(
      mergeMap(x => this.getFiles(x.ref)),
      mergeMap((listReferences: { getDownloadUrl: () => Promise<string> }[]) => {
        return forkJoin(listReferences.map(x => x.getDownloadUrl()))
      })
    )
    .subscribe(x =>  this.urls = x)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM