Ang*_*arM 4 firebase geofire angular google-cloud-firestore geofirestore
技术:我正在使用 Angular 7、Firestore、GeoFireX。
我想要的结果:如果用户需要该查询,我想添加 .where 查询。例如下面我有四个 .wheres。有时我可能只需要两个,因此如果用户按部门和品牌搜索,我只想使用 if 语句添加两个(参见方法:basicQueryBuilder)
下面的代码有效,但不是动态的。
此外,来自 GeoFireX 的 this.geo.collection 是否可以像普通集合一样具有 .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));
}
Run Code Online (Sandbox Code Playgroud)
第二次尝试:
let query = this.angularFirestore.collection('jobs');
query = query.where('sector', '==', 'teacher');
query = query.where('brand', '==', 'all');
return query.valueChanges();
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
错误 TS2322:“查询”类型不可分配给“CollectionReference”类型。
小智 5
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;
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1966 次 |
| 最近记录: |