英文:
filter rxjs operator on result valuechanges
问题
我正在从 Angular (14.2) 查询一个 Firestore (7.5.0) 数据库。where 子句只允许一个范围/不等式谓词,所以我尝试使用 filter 来影响第二个。这段代码
let project$: Observable<Project[]> = this.firestore.collection<Project>('Projects',
ref => ref.where('EndDt', '>=', startDt)).
valueChanges().pipe(filter((proj) => { // 一次获取整个数组,所以要么全部要么都不要
console.log('proj: ', proj) ;
return proj['StartDt'] <= endDt ;
})) ;
一次性将所有结果(110个文档)发送给了 filter,因此我的真假返回看起来像是一切或无。我这样结构是否有误?谢谢,
英文:
I am querying a firestore (7.5.0) db 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 thru the array ... but again it's all or nothing). Am I structuring this wrong? Thanks,
答案1
得分: 2
RxJs的筛选操作符不会筛选响应,它会筛选流。如果一个值通过了筛选函数,那么流会发出该值,否则流不会发出。如果你想筛选结果,你需要使用RxJs的映射操作符来转换发出的值,并在发出的数组上使用筛选器。
obs$.pipe(map(results => results.filter(val => filterFunction(val))));
英文:
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 other wise 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))));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论