1

I'm trying to wrap my head around RXJS.

I'm getting an array of Things via an http request.

But I'm having trouble taking a number of those objects.

Here's my method.

getThingsById(ID: number): Observable<IThing[]> {
return this._http.get(this.URL + ID + '/things)
  .map((response: Response) => <IThing[]>response.json())
  .take(20);
}

When I try to take 20 of those things I get all of them.

What am I doing wrong?

Thanks!

3
  • 1
    .take(20) operates on the stream of arrays, not on each array in that stream... Commented Sep 11, 2017 at 23:06
  • I only use take(1); which roughly means you get the result once and stop listening Commented Sep 11, 2017 at 23:11
  • You don't need to take() anything since NG2 does it for you. Look at the HttpService method : i.sstatic.net/GLkR7.png. You can take your response from the parsed json and use the Array .filter() method Commented Sep 11, 2017 at 23:14

2 Answers 2

2

try Array.slice() method:

getThingsById(ID: number): Observable<IThing[]> {
  return this._http.get(`${this.URL}${ID}/things`)
    .map((response: Response) => <IThing[]>response.json().slice(0, 20));
}
Sign up to request clarification or add additional context in comments.

Comments

1

Observables don't appear to work as described when using it with Http. With Http there is ONE response that comes back from the server. That ONE response has the entire set of requested data.

So the Observable contains the entire set of requested data from the ONE response.

1 Comment

Thanks for the response @Deborah! I enjoyed your Pluralsight course on Angular.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.