0

I have a switch map with an inner map function which is returning multiple obersables like so:

  this.fb
    .getUsersGroupsAsObservable(user.uid, 'contacts')
    .switchMap(groups => {
      return groups.map(group => this.fb.getJobsbyGroup(group.id));
    })
    .subscribe(res => {
      console.log(res); // emits Observable {_isScalar: false, source: Observable, operator: MapOperator}
      this.job$ = res;
    });
});

This is running twice correctly and returning two observables like so:

Observable {_isScalar: false, source: Observable, operator: MapOperator}
Observable {_isScalar: false, source: Observable, operator: MapOperator}

Is there any way to return a list of observables (thinking AngularFireList) which I can then render with an async pipe?

Update: @concat helped me to get an answer to this (but for reason the answer was deleted), I updated my code as follows (its now working perfectly):

  this.fb
    .getUsersGroupsAsObservable(user.uid, 'contacts')
    .switchMap(groups =>
      combineLatest(groups.map(group => this.fb.getJobsbyGroup(group.id))),
    )
    .subscribe(res => {
      console.log(res);
      this.jobs = [].concat.apply([], res);
    });

1 Answer 1

2

@concat helped me to solve this problem, all I needed to do was to use combineLatest and that would combine all of my arrays into a single array, I then flattened that array, there might be a simpler way to do this but hey it works:

  this.fb
    .getUsersGroupsAsObservable(user.uid, 'contacts')
    .switchMap(groups =>
      combineLatest(groups.map(group => this.fb.getJobsbyGroup(group.id))),
    )
    .subscribe(res => {
      console.log(res);
      this.jobs = [].concat.apply([], res);
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.