是否可以在AngularFire2中执行多查询,或者我是否可以改变数据客户端?

时间:2018-03-31 17:30:12

标签: firebase firebase-realtime-database ionic3 observable angularfire2

我有一个餐馆列表,我想使用过滤器,用户可以过滤餐馆列表。所以这是过滤器对象的数据:

dishType:(3) ["Pasta", "Barbecue", "Casual Dining"]
dishnationality:(4) ["Korean", "Indian", "Arabian", "German"]
organizeby:"Low Price"

在这里,我已经有了一个查询orgranizing他们大多数评价如下:

export class RestaurantService {
  restaurantsRef: AngularFireList<any>;
  restaurantsList: Observable<any[]>;

  constructor(
    public afDb: AngularFireDatabase,
    public storage: Storage
    // public homePage: HomePage
  ) {
    this.restaurantsRef = afDb.list('/restaurants');
    this.restaurantsList = this.restaurantsRef.valueChanges();
  }

getFilters(filters) {
  if (filters.organizeby === "Most rating") {
     this.restaurantsRef = this.afDb.list('/restaurants', ref => ref.orderByChild('rating'));        
     this.restaurantsList = this.restaurantsRef.valueChanges();
  }

   return this.restaurantsList;
}

但是如何根据我上面显示的过滤器对象进行多重查询?

或者我是否必须在firebase中考虑另一种方法,例如将数据库存储在数组中以迭代匹配值?

我试图在我的firebase db中创建一个composite_key:

getFilters(filters) {

    let dishtype = filters.dishtype;
    let dishnationality = filters.dishnationality;
    let composite_key = `${dishnationality}_${dishtype}`;

    console.log(dishtype, dishnationality)
    if(composite_key){
        this.restaurantsRef = this.afDb.list('/restaurants', ref => ref.orderByChild('tag_dishnationality').equalTo(composite_key));
        this.restaurantsList = this.restaurantsRef.valueChanges();
    }


    if (filters.organizeby === "Most rating") {
        this.restaurantsRef = this.afDb.list('/restaurants', ref => ref.orderByChild('rating'));
        this.restaurantsList = this.restaurantsRef.valueChanges();
    }


    return this.restaurantsList;
}

但是如何处理多个数组值并将它们组合起来?

1 个答案:

答案 0 :(得分:1)

如果我找对你,你正在考虑是否应该从服务器进行多次查询

我会说你不应该对服务器进行查询,因为它更容易FireBase构造通常会过滤单个查询

在这种情况下,创建模型以通过使用单个参数进行过滤来获取服务器的值,将响应作为数组获取,并在数据到达客户端时对数组执行多个查询

这比向服务器抛出多个查询快速而简单,因为如果没有正确绑定,可能会导致致命错误