0

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.

Morton
  • 5,380
  • 18
  • 63
  • 118

1 Answers1

1

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

rguerin
  • 2,068
  • 14
  • 29