我正在尝试在我的 AngularFire 应用程序中使用用户搜索功能。 由于 firestore 不支持这些查询,我认为单独查询字段就足够了
getUsersByName(searchValue: string) {
const firstNames = this.afs.collection<IUser>('user', ref => ref.orderBy('firstname').startAt(searchValue).endAt(searchValue+'\uf8ff')).valueChanges({ idField: 'id' });
const lastNames = this.afs.collection<IUser>('user', ref => ref.orderBy('lastname').startAt(searchValue).endAt(searchValue+'\uf8ff')).valueChanges({ idField: 'id' });
return concat(firstNames, lastNames);
}
但这仅适用于 firstNames
。仅使用第一个 Observable。我想我不理解 concat 运算符,但根据文档,我不清楚当前针对此问题的最佳解决方案是什么。
答案 0 :(得分:0)
您可以使用 zip 运算符
const firstNames: Observable<string>
const lastNames: Observable<string>
zip(firstNames,lastNames).subscribe(
([firstName,lastName]) => { console.log(firstName,lastName);}
)
如果 firstNames 和 lastNames 只发出一项, combineLatest([firstNames,lastNames]) 将更具可读性
了解如何使用这些运算符 https://indepth.dev/posts/1114/learn-to-combine-rxjs-sequences-with-super-intuitive-interactive-diagrams
的重要链接答案 1 :(得分:0)
这仅适用于名字的原因是因为 concat
的工作方式;它一次只会使用一个 observable 直到它完成,但是 firestore observable 是长期存在的并且不会完成。
您应该使用 merge
而不是 concat
。
import { merge } from 'rxjs';
getUsersByName(searchValue: string) {
const firstNames = this.afs.collection<IUser>('user', ref => ref.orderBy('firstname').startAt(searchValue).endAt(searchValue+'\uf8ff')).valueChanges({ idField: 'id' });
const lastNames = this.afs.collection<IUser>('user', ref => ref.orderBy('lastname').startAt(searchValue).endAt(searchValue+'\uf8ff')).valueChanges({ idField: 'id' });
return merge(firstNames, lastNames);
}