0

I use the code with rxjs 5.5.12 in React Native, it works.

In rxjs 5.5.12:

// the function will return Observable
  rxInit() {
    return Observable.combineLatest(
      myObservable1,
      myObservable2,
    ).flatMap((result) => {
      console.log('rxInit result =>', result) // I can see the result
      const [token, email] = result

      this.token = token
      this.email = email

      // check the value is empty or not and return Observable.

      if (this.token != null && this.email != null) {
        return Observable.fromPromise(myApiPromise).catch(handleErrorFunction)
      } else if (this.token != null && this.uid != null) {
        return Observable.fromPromise(myApiPromise).catch(handleErrorFunction)
      } else {
        return Observable.of(null)
      }
    })
  }

In rxjs 6.5.3:

import some oerator first:

import { combineLatest } from 'rxjs';
import { flatMap } from 'rxjs/operators';

I change the code:

rxInit() {
  console.log('rxInit start');

  return combineLatest(
    myObservable1,
    myObservable2
   ).flatMap((result) => {
     console.log('rxInit result =>', result)
   });

   console.log('rxInit end');
 }

It will show error TypeError: (0 , _rxjs.combineLatest)(...).flatMap is not a function.

So I note that may be I have to use pipe, I try to change the code.

rxInit() {
    console.log('rxInit start'); // it works.

    return combineLatest(
      myObservable1,
      myObservable2
    ).pipe(flatMap((result) => {
      console.log('rxInit result =>', result);  // the console log doesn't work
    }));
    console.log('rxInit end'); // the console log doesn't work
  }

I have no idea why I can't get the result in my console.log.

Any help would be appreciated.

Morton
  • 5,380
  • 18
  • 63
  • 118

1 Answers1

3

looks like you are not returning anything from flatMap(), mergeMap is used in rxjs in favor of flatMap btw. You need to return an observable.

return combineLatest(
  myObservable1,
  myObservable2
).pipe(mergeMap((result) => {
  console.log('rxInit result =>', result);  // the console log doesn't work
  return of(result)
}));
Fan Cheung
  • 10,745
  • 3
  • 17
  • 39
  • Thanks you so much ! I try add the return and subscribe it (I forgot to subscribe), it works. – Morton Oct 30 '19 at 08:43