How to subscribe multiple observable in rxjs 6 ? do is not a function in rxjs 6

Morton

In rxjs 5.5.12, I created a login() to subscribe with multiple observable by do()

login(userEmail, userPassword).subscribe((response) => {
  console.log(response);

  if (response === 'something') {
    // some actions
  } else {
    // some actions
  }
},(error) => {
  // some error actions
});

I use rxLoginPOST to call api:

rxLoginPOST(url, params) {
  return Rx.Observable.fromPromise(somePromise).catch(handleErrFunction);
}

I use it in login function it return multiple observable:

login(email, password) {
  return APIService.shared.rxLoginPOST('users/signin', { ...arguments })
  .flatMap((response) => {
    if (response.status === 200) {
        return Rx.Observable.combineLatest(
          myObservable1,
          myObservable2,
          myObservable3
        )
      } else {
        return Rx.Observable.of(response)
      }
  }).do((result) => {
      if (result.length > 0) {
        // some logic
      } else {
        return Rx.Observable.of(result)
      }
  }).flatMap((result) => {
      if (result.length > 0) {
        return this.checkUserIsRegistered()
      } else {
        return Rx.Observable.of(result)
      }
  })
}

In rxjs 6.5.3, I had changed all the import like

import { Observable, combineLatest, of } from 'rxjs';
import { mergeMap, map, catchError, flatMap, tap } from 'rxjs/operators';

If I trigger login() it will show do is not a function

So I change the code:

login(userEmail, password) {
    return APIService.shared.rxLoginPOST('users/signin', { ...arguments }).pipe(
        mergeMap((response) => {
        if (response.status === 200) {
          return combineLatest(
            myObservable1,
            myObservable2,
            myObservable3
          )
        } else {
          return of(response)
        }
      }).tap((result) => { // I am stuck here, I have no idea how to continue with it...
         console.log('check 4');  // check 4 does not appear.
         console.log('response', response);
         return of(response)
      }
    );

I try to use tap instead of do, but check 4 does not appear.

Any help would be appreciated.

rguerin

With pipe, you should chain your operators like this: pipe(op1, op2) and not like pipe(op1).op2()

login(userEmail, password) {
    return APIService.shared.rxLoginPOST('users/signin', { ...arguments })
      .pipe(
          mergeMap((response) => {
            if (response.status === 200) {
              return combineLatest(
                myObservable1,
                myObservable2,
                myObservable3
              )
            }
            return of(response)
          }),
          tap((result) => { 
            console.log('check 4');  // check 4 does not appear.
            console.log('response', response);
          })
        );

I also removed your useless else statement since you are returning something before :)

EDIT: As suggested in comments, the return instruction from tap has also been removed since unecessary

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

rxjs6 ActivatedRoute subscribe to observable

How do you sort an Observable<Item[]> in Angular 6 with rxjs 6?

Using Angular, RxJs 6 and InMemoryAPI how do I get a returned Observable value that seems to be nested in multiple observable sources?

How to start observable in the pipe in RxJS 6?

How to return value from subscribe of forkJoin with angular 6 and RxJs 6?

rxjs 6 countdown timer subscribe angular 6

How to keep observable alive after error in RxJS 6 and Angular 6

RxJS - subscribe only once but do not complete Observable

Refactor nested subscribe with conditional rxjs6

How do I mock RxJs 6 timer?

how to subscribe to the last observable and execute a funtion in rxjs?

RxJs 6 chaining multiple subscriptions

How to handle error and return observable while subscribe inside an observable function in Rxjs Angular

angular 6 return result of forkJoin subscribe (rxJs 6)

RxJS 6 filter and map an observable array of items

rxjs6 filter Observable<Array<any>>

RxJs 6: Get ConnectableObservable from Observable

RxJS 6 get a filtered list of array of Observable

subscribe keys of and observable in Angular/RxJS

How to set context to rxjs subscribe callback function?

how to pass object to rxjs subscribe() callback function?

How to use race with rxjs 6

Angular 6 call subscribe() for Observable multiple times

Subscribe to child Observable's Subscribe in RxJS

How to throwError within map of observable (rxjs6, ng6)

Could not use Observable.of in RxJs 6 and Angular 6

Angular 6 / RxJs 6 Observable merging that also skips to error block

How to chain switchMap-operators with function bodies in rxjs 6

Angular8 rxjs6 how to wait for results of subscribe in a service