Currently, I am in the process of learning about Angular 2, TypeScript, RxJs, and more. However, I have encountered an issue while attempting to return a subset of data within a service utilizing RxJs and Observables.
My expectation with the getCars function is that it will retrieve a JSON file, parse the data, and return only a portion of it based on specified offset and count parameters. Unfortunately, the problem lies in the fact that I am consistently receiving all of the data back (even though the file contains 200 entities/cars).
I am unsure of what mistake I might be making in this scenario.
EntityService
@Injectable()
export class EntityService {
constructor(private http: Http) { }
getCars(offset: number, count: number): Observable<Car[]> {
return this.http
.get('resources/data/cars.json')
.map(this.extractData)
.skip(offset)
.take(count)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body.data || {};
}
private handleError(error: any) {
// ...
}
}
cars.json
{
"data":[
{
"vin":"ee8a89d8",
"brand":"Fiat",
"year":1987,
"color":"Maroon"
},
{
"vin":"642b3edc",
"brand":"Renault",
"year":1968,
"color":"White"
}
]
}