Issue
I am querying a Firestore (7.5.0) database from Angular (14.2). The where clause(s) allow only one range/inequality predicate, so I am trying to use filter to affect the second. This code
let project$: Observable<Project[]> = this.firestore.collection<Project>('Projects',
ref => ref.where('EndDt', '>=', startDt)).
valueChanges().pipe(filter((proj) => { // Gets whole array once so all or nothing
console.log('proj: ', proj) ;
return proj['StartDt'] <= endDt ;
})) ;
Sends filter the entire result (110 documents) all at once, so my true or false return seems like an all or nothing (I could iterate through the array, but again it's all or nothing). Am I structuring this wrong?
Solution
The RxJs filter operator does not filter the response; it filters the stream. If a value passes the filter function then the stream emits the value. Otherwise, the stream doesn't emit. If you want to filter the results, you need to use the RxJs map operator to transform the emitted value and use the filter on the emitted array.
obs$.pipe(map(results => results.filter(val => filterFunction(val))));
Answered By - Adrian Brand
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.