【问题标题】:Angular2Firebase query by multiple values whereINAngular2Firebase 通过多个值查询 whereIN
【发布时间】:2018-07-28 11:40:29
【问题描述】:

所以我正在寻找 WHERE IN(val1,val2) 的等效项,我已经尝试过这样但不起作用。

Angular Firebase

this.firebase.database.list('/favorites', {
            query: {
                orderByChild: 'uID',
                equalTo: [1,2,3,'some value]
            }
});

【问题讨论】:

  • 是否等于你需要一个不同值的数组? equalto 仅适用于一个值。这种类型的配置。要求您以不同方式设置 Firebase。需要知道你想要完成的总体目标
  • 我的主要目标是按 id 查询一组用户并列出他们,我试图按用户列出最喜欢的业务,所以我需要同时按多个 id 查询。所以是的,不同的价值观@JesseTeWeehi
  • 您应该将收藏夹保存在用户节点下。那么就很容易了。

标签: angular firebase ionic-framework


【解决方案1】:

Firebase 的查询没有 ORIN 运算符。因此,您无法轻松地将上述查询映射到 Firebase。

一种解决方法是在元素上实现 multiFilter

public items: FirebaseListObservable<any[]> = null;


ngOnInit() {
    this.items = this.db.list('/favorites', {
        query: {
            orderByChild: 'uID'  
        }
    });

    this.items.subscribe(items => {
       console.info(this.multiFilter(items,{'uID': [1,2,3,'some value]}));
    });
}

public multiFilter(arr: Object[], filters: Object) {
  const filterKeys = Object.keys(filters);
  return arr.filter(eachObj => {
    return filterKeys.every(eachKey => {
      if (!filters[eachKey].length) {
        return true; // passing an empty filter means that filter is ignored.
      }
      return filters[eachKey].includes(eachObj[eachKey]);
    });
  });
};

【讨论】:

    【解决方案2】:

    查询是通过在 firebase.database.Reference 上构建的。 查看文档:https://github.com/angular/angularfire2/blob/master/docs/rtdb/querying-lists.md

      this.firebase.database.list('/favorites', {
        ref => ref.orderByChild('uID').equalTo(1,2,3,'some value)
    
       });
    

        constructorpublic af:AngularFireDatabase) {
                 this.items = af.list('/messages', ref => 
                   ref.orderByKey(true).equalTo(1,2,3,'some value));
            });
    

    【讨论】:

    • @ChellappanV 我认为你的回答应该是编辑 this.firebase.database.list('/favorites', ref => ref.orderByChild('uID').equalTo(1,2,3 ,'一些值'));在某些值之后,您有额外的车把和缺少的逗号。但你的答案似乎是好的一个
    【解决方案3】:

    我会让用户在用户节点下。

    ID 通常是 firebase pushKeys。

    so user > userId > {用户信息}。

    那就有生意了

    业务 > 业务 ID > { 业务信息 }

    然后是企业到用户节点。

    businessForUser > userId > businessId = true

    因此,您将从 businessForUser 节点获取用户的业务 ID 列表,然后从具有这些获取的 ID 的业务节点获取业务信息。

    【讨论】:

      猜你喜欢
      • 2011-01-21
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多