Angular2Firebase通过多个值whereIN查询

前端之家收集整理的这篇文章主要介绍了Angular2Firebase通过多个值whereIN查询前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我正在寻找相当于WHERE IN(val1,val2)我试过这样但是不起作用.

Angular Firebase

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

解决方法

Firebase的查询没有OR或IN运算符.因此,您无法轻松将上述查询映射到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,'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]);
    });
  });
};

猜你在找的Angularjs相关文章