Firestore - 我想动态地将where语句添加到我的集合中

问题描述 投票:0回答:1

Tech:我正在使用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));
  }

第二次尝试:

  let query = this.angularFirestore.collection('jobs');
  query = query.where('sector', '==', 'teacher');
  query = query.where('brand', '==', 'all');

  return query.valueChanges();

我得到的错误:

错误TS2322:类型'Query'不能分配给'CollectionReference'类型。

angular firebase google-cloud-firestore geofire geofirestore
1个回答
0
投票
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;
    })
© www.soinside.com 2019 - 2024. All rights reserved.