可选参数和子句 .where
Optional parameter and clausule .where
我正在使用 angularcli
和 angularfire2
,我的应用程序有一个带有可选参数的方法来检索 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();
}
我只显示了 2 个参数以简化此示例,但在实际方法中我有 10 个参数。当最后一个参数是 null
时,有一个最好的检查方法或忽略 .where 条款
更新:
...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();
});
}
在工厂内的 ref 上构建,检查参数是否存在:
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;
});
我正在使用 angularcli
和 angularfire2
,我的应用程序有一个带有可选参数的方法来检索 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();
}
我只显示了 2 个参数以简化此示例,但在实际方法中我有 10 个参数。当最后一个参数是 null
更新:
...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();
});
}
在工厂内的 ref 上构建,检查参数是否存在:
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;
});