0

I have the following piece of code:

public getDomains(): Observable<any[]> {
    const customer = this.storage.getCustomer();
    const apiUrl= this.configuration.getApiUrl(customer);
    return this.http.get((`${apiUrl}/domains`).toLowerCase())
        .map((response) => response.json() || []);
}

const domains = this.myService.getDomains().
    .shareReplay()
    .concatMap((domains) => domains.map((domain) => this.toDomainModel(domain, myObservable)))
    .filter((domain) => this.isValidDomain(domain))
    .toArray();

My network tab is showing the response from the request as an array with 3 items in it. But somehow, even after removing everything and only leaving the .toArray(), it is transformed into an Array with 3 elements, where each element itself is an Array of 3 items. The network response is being duplicated 4 times somewhere.

I call a service method which returns an array of items which I then send through this chain of methods. For some reason, and I believe it may have to do with concatMap, I end up with more items than the endpoint originally returned.

I should get 3 items back from the request, and then the filter should remove 2 of those so I should be left with one. I'm fairly new to rxjs and am curious as to whether or not this is common behavior with concatMap.

Any ideas? Thanks

User 5842
  • 2,849
  • 7
  • 33
  • 51
  • Please share a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Ele Jan 26 '18 at 22:48
  • I've added more code @EleazarEnrique, is this enough? I can provide more if need be – User 5842 Jan 26 '18 at 22:54
  • Your current output and expected output would be helpful. – Jack Guy Jan 26 '18 at 23:01
  • I added more text @Harangue – User 5842 Jan 26 '18 at 23:06
  • @IngoBürk Nope, you can give it an iterable and it will flatten it for you. The way it's being used here is a little strange though. https://stackoverflow.com/a/42482824/1008741 – Jack Guy Jan 26 '18 at 23:08
  • Yeah, I noticed now as well. Deleted my comment. :) – Ingo Bürk Jan 26 '18 at 23:09
  • Sorry @User5842 but there's nothing obviously wrong with your observable, especially with the context we're given. If I had to hazard a guess I would say the issue is related to your subscriptions, but if directly `toArray`'ing the network response is still exhibiting the problem, there's no insight to be had from the code you posted. – Jack Guy Jan 26 '18 at 23:14

0 Answers0