如何根据条件更改此查询
let query = (ref: CollectionReference)=>{
return ref.where("dateOrd", ">=", this.dateIn)
.where("dateOrd", "<=", this.dateOu)
.where("status", "==", this.staTus)
.where("seller", "==", this.idseller)
.where("ptype", "in", this.ptype)
.orderBy("dateOrd", "desc")
.orderBy("creado", "desc")
.limit(5000)
}
例如,如果满足条件,则更改查询
类似的东西(此操作失败(
if (typeof this.idCli!="undefined"){
query = query.where("client", "==", this.idCli)
}
但是,我该如何进行多重查询?
请帮帮我。谢谢
Firestore查询遵循生成器模式,其中对where()
的每次调用都返回一个新查询。
所以你只需重复呼叫where
,就像在你的第二个片段:中一样
let query = (ref: CollectionReference)=>{
let q = ref.where("dateOrd", ">=", this.dateIn)
.where("dateOrd", "<=", this.dateOu)
.where("status", "==", this.staTus)
.where("seller", "==", this.idseller)
.where("ptype", "in", this.ptype)
.orderBy("dateOrd", "desc")
.orderBy("creado", "desc")
.limit(5000)
if (typeof this.idCli!="undefined"){
q = q.where("client", "==", this.idCli)
}
return q;
}
如果你的其他条件也是动态的,你可以对它们做同样的事情:
let q = ref;
if (typeof this.dateIn!="undefined"){
q = q.where("dateOrd", ">=", this.dateIn)
}
if (typeof this.dateOu!="undefined"){
q = q.where("dateOrd", "<=", this.dateOu)
}
...
q = q.limit(5000)
if (typeof this.idCli!="undefined"){
q = q.where("client", "==", this.idCli)
}
return q;