技术:--我用的是角7,Firestore,GeoFireX。
我想要的结果:如果用户需要查询,我想添加.where查询。例如,下面有四个.wheres。有时我可能只需要两个,因此,如果用户按部门和品牌进行搜索,我只想使用if语句添加两个语句(参见方法: basicQueryBuilder)
下面的代码可以工作,但不是动态的。
此外,来自this.geo.collection的GeoFireX是否有一个.limit(20),就像正常的集合一样?
当前尝试:
public getFirestoreJobs(queryType: string, limit: number, pageSize: number): Observable<any> {
limit = limit + pageSize;
if (queryType === 'geo') {
const collection = this.geoQueryBuilder();
const center = this.geo.point(51.5074, 0.1278);
const radius = 20;
return collection.within(center, radius, 'point');
} else {
const collection = this.basicQueryBuilder();
return collection.valueChanges();
}
}
public geoQueryBuilder(): any {
return this.geo.collection('jobs', ref => ref
.where('sector', '==', 'teacher')
.where('brand', '==', 'all')
.where('payType', '==', 'salary')
.where('tags', 'array-contains', 'salary'));
}
public basicQueryBuilder(): any {
return this.angularFirestore.collection('jobs', ref => ref
.where('sector', '==', 'teacher')
.where('brand', '==', 'all')
.where('payType', '==', 'salary')
.where('tags', 'array-contains', 'salary').limit(20));
}第二次尝试:
let query = this.angularFirestore.collection('jobs');
query = query.where('sector', '==', 'teacher');
query = query.where('brand', '==', 'all');
return query.valueChanges();我得到的错误:
错误TS2322:输入‘查询’不能分配到键入‘收集器’。
发布于 2019-02-16 18:37:24
import {Query, QueryFn } from '@angular/fire/firestore';
this.angularFirestore.collection('jobs', ref => {
let query : Query = ref;
if(condition1) {
query = query.where('sector', '==', 'teacher');
}
if(condition2) {
query = query.where('brand', '==', 'all');
}
return query;
})https://stackoverflow.com/questions/54659300
复制相似问题