Angular 2 Observables - Return original observable, instead of switchMap/combineLatest

Stevie Star

I have a function that makes some calls to a Firebase database that aggregates a bunch of user profile data together. The one issue I'm running into is if the users don't have a videos property on their user object, the observable errors out.

Right now, I'm checking to see if there are any videos on the user object. If there are, it returns a new observable that combines a bunch of observables based on the data that it fetches from that videos property (an array). However, if there aren't, I need to return the original observable before the .switchMap().

Does anyone know what I need to tweak here?

Here's my function:

getUserProfile(id) {
    return this._af.database
      .object(`/social/users/${id}`)

      // Switch to the joined observable

      .switchMap((user) => {

        let vidKeys = Object.keys(user.videos);

        if(vidKeys && vidKeys.length > 0) {
          // Use forkJoin to join the video observables. The observables will
          // need to complete, so first is used. And use forkJoin's selector to
          // map the videos to the user and then return the user.

          return Observable.combineLatest(
            vidKeys.map((vidKey) => this._af.database
              .object(`/social/videos/${vidKey}`)
            ),
            (...videos) => {
              vidKeys.forEach((vidKey, index) => {

                // Query YouTube api to get the duration
                this._yt.getVideo(vidKey).subscribe(data => {

                  // Format the duration
                  let duration = this._yt.convertToStandardDuration(data.items[0].contentDetails.duration)
                  videos[index]['duration'] = duration;
                  user.videos[vidKey] = videos[index];

                });

               });
              return user;
            }
          )
        } else {

          // TO-DO: Return original observable that was called before the .switchMap()

        }
      });
  }
cartant

You don't have to return the original observable, you only have to return an observable that emits the original value, as the switchMap will be invoked when original observable re-emits. You can do that with Observable.of:

import 'rxjs/add/observable/of';

getUserProfile(id) {
  return this._af.database
    .object(`/social/users/${id}`)
    .switchMap((user) => {

      let vidKeys = Object.keys(user.videos);

      if(vidKeys && vidKeys.length > 0) {
        ...
      } else {
        return Observable.of(user);
      }
    });
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Mocking service calls that return observables in Angular 2?

分類Dev

Angular 2 Observables

分類Dev

Angular 2/4 Observables

分類Dev

Angular4 returning Observable with nested Observables

分類Dev

Angular 6 return Observable

分類Dev

Angular 2 Firebase Observable to promise doesn't return anything

分類Dev

Angular 2 Observables-switchMap / CombineLatestの代わりに、元のobservableを返します

分類Dev

Rxjs combine 2 observables, Angular 2

分類Dev

Angular 2 temlate with Observable

分類Dev

Fetch data once with Observables in Angular 2

分類Dev

Angular 2ObservableからObservable []

分類Dev

Angular Observables

分類Dev

Angular 2: Convert Observable to Promise

分類Dev

Angular 2: Convert Observable to Promise

分類Dev

Angular2 ChangeDetection or Observable?

分類Dev

Why to use observables instead of Ajax?

分類Dev

Angular 2 - Sort list from Observable

分類Dev

Angular 2 http get observable called twice

分類Dev

How to modify data in Observable in Angular 2?

分類Dev

Angular 2: Observable subscription not reading data correctly

分類Dev

Angular 2+ Observable と GroupBy

分類Dev

Angular:ForkJoin ngrx observables

分類Dev

Angular HttpClient&Observables

分類Dev

Angular 6 Observables

分類Dev

filter a single value with RxJS in and return an observable and use the observable via async in a template in Angular

分類Dev

Merge multiple observables and return one

分類Dev

how to handle observable errors but continue with follow on observables?

分類Dev

rx java observable chaining on only matched observables

分類Dev

Combine 2 Observables rxjs

Related 関連記事

ホットタグ

アーカイブ