本文介绍了可选参数和条款 .where的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 angularcliangularfire2,我的应用程序有一个带有可选参数的方法,用于在 firestore 中检索数据.

buildWomen(id: string, sex?: string, age?: string): Observable{this.humanCollec = this.db.collection('human/' + id + '/women', ref => ref.where('sex', '==', sex)//--可以为空.where('年龄', '==', 年龄);//-- 这可以为空返回 this.humamObersArray = this.humanCollec.valueChanges();}

为了简化这个例子,我只显示了 2 个参数,但在实际方法中我有 10 个参数.当最后一个参数为 null

时,有一种检查它或忽略 .where 子句的最佳方法

更新:

...service.ts

...humanCol: AngularFirestoreCollection<Human>;humanObersArray: Observable;...buildHuman(id:string,sex?:string,age?:string,ethnicity?:string,height?:string,weight?:string,religion?:string){//:Observable<Human[]>//this.humanCol =this.db.collection('human/'+id+'/women', ref => {让 retVal = ref 为任何;if (sex != null) { retVal = retVal.where('sex', '==', sex) }if (age != null) { retVal = retVal.where('age', '==', age) }if (ethnicity != null) { retVal = retVal.where('ethnicity', '==',ethnicity) }if (height != null) { retVal = retVal.where('height', '==', height) }if (weight != null) { retVal = retVal.where('weight', '==', weight) }如果(宗教!= null){ retVal = retVal.where('宗教','==',宗教)}返回 retVal;//返回 this.humanObersArray = this.humanCol.valueChanges();});}
解决方案

在工厂内的 ref 上构建,检查参数是否存在:

this.humanCollec = this.db.collection(`human/${id}/women`, ref => {让 retVal = ref 为任何;if (sex != null) { retVal = retVal.where('sex', '==', sex) }if (age != null) { retVal = retVal.where('age', '==', age) }...返回 retVal;});

I'm using angularcli and angularfire2, my app have a method with optional parameter for to retrieve data in firestore.

buildWomen(id: string, sex?: string, age?: string): Observable<Humans[]> {
    this.humanCollec = this.db.collection('human/' + id + '/women', ref => ref
      .where('sex', '==', sex) //--this can be null
      .where('age', '==', age); //--this can be null
    return this.humamObersArray = this.humanCollec.valueChanges();
  }

I'm showing only 2 parameters for simplify this example but in real method I have 10 parameters. There is a the best way for check it or ignore .where clausule when last parameter is null

UPDATE:

...service.ts

...
humanCol: AngularFirestoreCollection<Human>;
humanObersArray: Observable<Human[]>;
...
buildHuman(id: string, sex?: string, age?: string, ethnicity?: string, height?: string, weight?: string, religion?: string){ //: Observable<Human[]>
      //this.humanCol =

      this.db.collection('human/'+id+'/women', ref => {
      let retVal = ref as any;
      if (sex != null) { retVal = retVal.where('sex', '==', sex) }
      if (age != null) { retVal = retVal.where('age', '==', age) }
      if (ethnicity != null) { retVal = retVal.where('ethnicity', '==', ethnicity) }
      if (height != null) { retVal = retVal.where('height', '==', height) }
      if (weight != null) { retVal = retVal.where('weight', '==', weight) }
      if (religion != null) { retVal = retVal.where('religion', '==', religion) }

      return retVal;
      //return this.humanObersArray = this.humanCol.valueChanges();
    });
  }
解决方案

Build on the ref within the factory, checking for the presence of the parameters:

this.humanCollec = this.db.collection(`human/${id}/women`, ref => {
  let retVal = ref as any;
  if (sex != null) { retVal = retVal.where('sex', '==', sex) }
  if (age != null) { retVal = retVal.where('age', '==', age) }
  ...
  return retVal;
});

这篇关于可选参数和条款 .where的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 11:01