I make a call to a method called getWorkOrders() in my service file which in turn makes a call to the server to fetch the records.
Here is my service. I am using the new HttpClient.
export class BackendServices {
private BASE_URL ='http://localhost:3000/backend';
constructor(private http: HttpClient) {}
getWorkOrders(){
return this.http.get(this.BASE_URL + '/getworkorders/');
}
}
Component.ts file
private woSubject = new BehaviorSubject<IWorkOrders[]>([]);
getWorkOrders() {
this.bs.getWorkOrders()
.subscribe((data: any) =>
this.woSubject.next(data),
);
}
From the component getWorkOrders method how do I filter the data from all the records that was fetched from the server. I understand that it is using pipe & the filter rxjs operators, but not sure how to put it together.
pipe() can be called on one or more functions, each of which can take one argument ("UnaryFunction") and uses it to return a value. It returns a function that takes one argument, passes it to the first UnaryFunction, and then passes the result to the next one, passes that result to the next one, and so on.
Async pipelinkThe AsyncPipe subscribes to an observable or promise and returns the latest value it has emitted.
A Pipeable Operator is a function that takes an Observable as its input and returns another Observable.
If you want to filter the workorders that come from the server using an rxjs filter you will need to turn the array of workorders into an observable of workorders, eg.
export class BackendServices {
private BASE_URL ='http://localhost:3000/backend';
constructor(private http: HttpClient) {}
getWorkOrders(){
return this.http.get(this.BASE_URL + '/getworkorders/')
.pipe(map((data) => Observable.from(data));
}
}
private woSubject = new BehaviorSubject<IWorkOrders[]>([]);
getWorkOrders() {
this.bs.getWorkOrders()
.pipe(
filter(data => data.timestamp > 123456786 ),
toArray()
)
.subscribe((data: any) =>
this.woSubject.next(data),
);
}
The alternative is just to filter the array in a map using the standard array filter, eg.
export class BackendServices {
private BASE_URL ='http://localhost:3000/backend';
constructor(private http: HttpClient) {}
getWorkOrders(){
return this.http.get(this.BASE_URL + '/getworkorders/');
}
}
private woSubject = new BehaviorSubject<IWorkOrders[]>([]);
getWorkOrders() {
this.bs.getWorkOrders()
.pipe(map(data => data.filter(workorder => workrder.timestamp > 123456786) )
.subscribe((data: any) =>
this.woSubject.next(data),
);
}
One question I would ask is why you are pushing the result of the observable subscription into the behavior subject? Generally if you are pushing results from a subscription into another observable you can achieve the same thing by combining observables instead
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With