Rodrigo Villalobos
Rodrigo Villalobos

Reputation: 197

Why my subscribe function is printing an object instead of the result of the http request?

Why this code is printing this:

Observable {_isScalar: false, source: Observable, operator: MapOperator}

Instead of the array which is getting returned by my service?

    const trendingRentals: Observable<any> = this.rentalService.getRentals();
    const commonRentals: Observable<any> = this.rentalService.getCommonRentalsTest();
    const luxuryRentals: Observable<any> = this.rentalService.getLuxuryRentalsTest();

    const concatenation =  concat( [trendingRentals, commonRentals, luxuryRentals]);
    concatenation.subscribe(
      x => console.log(x)
    )

Upvotes: 0

Views: 149

Answers (3)

davidveen
davidveen

Reputation: 388

The original code you had calls

concat([obs1, obs2, ..., obsN])

on a technical level, this is simply a case of providing the wrong argument to the concat function, as it expects

concat(obs1, obs2, ..., obsN)

as Andrew also pointed out. You can read more about rxjs functions on learn-rxjs

To go into a tiny bit more depth: what concat does is return an observable from other observables. When concat is called upon execution, it executes each of the observables you put in, and waits until each one has completed in order before returning.

The Array [obs1, ..., obsN] is not an observable, hence the 'weird' response.

Upvotes: 1

Andrew Radulescu
Andrew Radulescu

Reputation: 1889

The concat static operator here is accepting Observable(s) as parameter(s) not an Array.

If you are passing an Array of Observables will give you back the nested observables.

Read the internet for a more comprehensive answer and find here and here some references.

Upvotes: 2

Rodrigo Villalobos
Rodrigo Villalobos

Reputation: 197

I modified const concatenation = concat( [trendingRentals, commonRentals, luxuryRentals]); for const concatenation = concat( trendingRentals, commonRentals, luxuryRentals); Now it's working perfectly, however i dont understan why... Can someone explain to me what is happenning?

Upvotes: 0

Related Questions