【问题标题】:Firestore dynamically build the queryFirestore 动态构建查询
【发布时间】:2019-04-08 04:41:16
【问题描述】:

我想创建一个动态构造查询的查询函数 - 但是我似乎可以让任何查询元素绑定到查询:

在这里,我创建了 observable,我将 ref 传递给查询构建函数,该函数添加了我需要搜索的所有内容:

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 子句或限制...?

【问题讨论】:

    标签: typescript firebase google-cloud-firestore


    【解决方案1】:

    应用 where 函数时,它会返回一个 Query 类型,您可以在其中继续添加过滤器...如果您尝试从 ref 获取结果,它将忽略您的过滤器。

    相反,您应该将查询保存在变量中并从中获取结果

    const query = ref.where(`fieldName`, '==', 'true');
    query.where(`fieldName2`, '==', 'someString');
    return await query.get();
    

    【讨论】:

      【解决方案2】:

      以下代码适用于我:

      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()
      }
      

      【讨论】:

        猜你喜欢
        • 2012-11-23
        • 2018-10-15
        • 2012-12-21
        • 2018-12-21
        • 1970-01-01
        • 1970-01-01
        • 2013-08-12
        • 2019-06-24
        • 2020-12-03
        相关资源
        最近更新 更多