我想创建一个可动态构造查询的查询功能-但是,我似乎可以将任何查询元素绑定到查询:
在此处创建可观察对象,将引用传递给查询构建函数,该函数将添加需要搜索的所有内容:
firebaseObservable = this.firestoreService.colWithIds$('bucket', ref => this.createFirebaseQuery(ref, this.filterParams.filterBy, pagination));
我在这里构造查询
createFirebaseQuery(ref: any, categories: string[], isPagination?: boolean) {
// Fetch the requests order by direction
const direction: OrderByDirection = this.filterParams.sortBy.direction === 'asc' ? 'asc' : 'desc';
// Add th category search criteria
for (const category of categories) {
ref.where(`categoriesTest.${category}`, '==', 'true');
}
// Order by selected type and direction
ref.orderBy(this.filterParams.sortBy.type, direction);
// Check if the requests skills are new page
if (isPagination) {
ref.startAfter(this.paginationCursor);
}
// Add a limit to the results being returned
ref.limit(20);
return ref;
}
这似乎从未对其应用任何where子句或限制...?
答案 0 :(得分:0)
应用where函数时,它将返回查询类型,您可以在其中继续添加过滤器...如果尝试从ref获取结果,它将忽略您的过滤器。
相反,您应该将查询保存在变量中并从中获取结果
const query = ref.where(`fieldName`, '==', 'true');
query.where(`fieldName2`, '==', 'someString');
return await query.get();
答案 1 :(得分:0)
以下代码对我有用:
myFunction(){
return this.afs.collection<Adocao>('adocao', ref => {
return ref.where('adotado', '==', false)
.where('filhote', '==', true)
.where('porte', '==', 'Médio')
.where('sexo', '==', 'Fêmea')
.where('tipo', '==', 'Gato')
})
.valueChanges()
}