Thomas Tran
Thomas Tran

Reputation: 11

Rxjs 6 flatten observable

I need your advice on how to convert the Rxjs 5 to Rxjs 6.

My code is below and I am not too sure why it did not work.

Rxjs 5

Observable.from([{amount:5},{amount:10}])
.flatMap(_ => depositService.deposit(depositAmount))
.toArray()
.subscribe(result => {
    console.log(result.length);
})

Rxjs 6

import { Observable, from, } from 'rxjs';
import { map, catchError, mergeMap} from 'rxjs/operators';
...

const source = from([{amount:5},{amount:10}]);
source
.pipe(mergeMap(_ => depositService.deposit(_.amount).toArray())
.subscribe(result => {
    console.log(result.length);
})

I got the error

You provided an invalid object where a stream was expected You can provide an Observable, Promise, Array, or Iterable.

Upvotes: 1

Views: 1770

Answers (1)

tom van green
tom van green

Reputation: 1734

I think toArray is an operator and should be passed in in the pipe.

import { Observable, from, } from 'rxjs';
import { map, catchError, mergeMap, toArray} from 'rxjs/operators';
...

const source = from([{amount:5},{amount:10}]);
source
.pipe(
    mergeMap(_ => depositService.deposit(_.amount)),
    toArray()
)
.subscribe(result => {
    console.log(result.length);
})

Upvotes: 7

Related Questions