通过映射另一个 Observable 返回一个 Observable [英] Return an Observable by mapping another Observable

查看:51
本文介绍了通过映射另一个 Observable 返回一个 Observable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试映射一个 observable,从我返回的 observable 中获取一个值,然后将该值提供给另一个 observable 并返回该结果.这是我目前所拥有的:

I am trying to map an observable, get a value from my returned observable then feed this value into another observable and return that result. Here is what I have so far:

  getJobsByUser(user: User): Observable<Job[]> {
    return this.getUsersGroupsAsObservable(user.uid, 'contacts').map(groups => {
      groups.map(group => {
        this.getJobsbyGroup(group.id);
      });
    });

  getJobsbyGroup(groupId: string): Observable<Job[]> {
    return this.afs
      .collection<Job>('jobs', ref => ref.where(`group.${groupId}`, '==', true))
      .valueChanges();
  }

  getUsersGroupsAsObservable(
    userId: string,
    type: string = 'users',
  ): Observable<Group[]> {
    return this.afs
      .collection<Group>('groups', ref =>
        ref.where(`${type}.${userId}`, '==', true),
      )
      .valueChanges();
  }

问题是打字稿表明我的 getJobsByUser 函数将返回一个类型为:void 的 observable.当我在我的模板上输出它时,我什么也没有得到或未定义.我觉得我需要使用 switchMap 但我对 rx/js 有点模糊.我不确定如何返回 Job[]

The problem is typescript is indicating that my getJobsByUser function will return an observable of type:void. When I do output it on my template I get nothing or undefined. I feel like I need to use switchMap but im a little fuzzy with rx/js. I am unsure how to return an Observable of type Job[]

更新:在@Pranay Rana 的帮助下,我现在正在返回数组,并且可以获得这样的第一个值:

Update: With help from @Pranay Rana I am now returning array, and can get the first value like this:

  getJobsByUser(user: User): Observable<Job[]> {
    return this.getUsersGroupsAsObservable(user.uid, 'contacts').pipe(
      mergeMap(groups => {
        // returns an array of groups - we need to map this
        return this.getJobsbyGroup(groups[0].id); // works with the first value - do we need another map here?
      }),
    );
  }

更新 2:我已经设法从 firestore 取回了一些数据,但它发出了多个 observables 而不是组合流:

Update 2: I have managed to get some data back from firestore, but it is emitting multiple observables rather than a combined stream:

this.fb.getUsersGroupsAsObservable(user.uid, 'contacts')
   .switchMap(groups => {
      return groups.map(group => this.fb.getJobsbyGroup(group.id));
   })
    .subscribe(res => {
       console.log(res);
       // this emits multiple observables rather than one
       this.job$ = res;
    });

推荐答案

以下方法详细讨论:处理并行多个请求的方式

下面的方法利用了mergemap

getJobsByUser(user: User) {
     return this.getUsersGroupsAsObservable(user.uid, 'contacts').pipe(
       mergeMap(group => this.getJobsbyGroup( group.id))
     );
}

callingfunction(){
  const requests = this.getJobsByUser(this.user);
  requests.subscribe(
  data => console.log(data), //process item or push it to array 
  err => console.log(err));
}

你也可以使用forkJoin

getJobsByUser(user: User) {
         return this.getUsersGroupsAsObservable(user.uid, 'contacts').pipe(
           map(group => this.getJobsbyGroup( group.id))
         );
    }

    callingfunction(){
      const requests = forkJoin(this.getJobsByUser(this.user));
      requests.subscribe(
      data => console.log(data), //process item or push it to array 
      err => console.log(err));
    }

这篇关于通过映射另一个 Observable 返回一个 Observable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆