AngularFirestore - 动态更新查询 [英] AngularFirestore - Update query dynamically

查看:21
本文介绍了AngularFirestore - 动态更新查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询:

loadData(settings: any): Observable<any[]> {
    const ref = this.db.collection('data').ref
      .orderBy('simpleID', 'desc')
      .where('isVerified', '==', true)
      .limit(5);
    // Gender filter
     if (settings.includeMales && !settings.includeFemales) {
       ref.where('gender', '==', 'm');
     } else if (!settings.includeMales && settings.includeFemales) {
       ref.where('gender', '==', 'f');
     }

return this.db.collection('confessions', ref => ref)
      .valueChanges();

我想根据 settings 对象动态应用过滤器.

I want to apply filters dynamically based on the settings object.

如何使用 ref 变量并将其传递给 Angularfire2 的 collect() 方法?

How can I use the ref variable and pass it to the collect() method of Angularfire2?

推荐答案

每次调用 where() 或其他方法时,它实际上都会返回一个新查询.所以你只需要确保捕获每个调用的查询.

Every time you call a where() or other method, it actually returns a new query. So you just need to make sure to capture the query of each call.

var query = this.db.collection('data').ref
  .orderBy('simpleID', 'desc')
  .where('isVerified', '==', true)
  .limit(5);
if (settings.includeMales && !settings.includeFemales) {
  query = query.where('gender', '==', 'm');
} else if (!settings.includeMales && settings.includeFemales) {
  query = query.where('gender', '==', 'f');
}

return this.db.collection('confessions', ref => query).valueChanges();

这篇关于AngularFirestore - 动态更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆