【问题标题】:Angular Firestore make this query change depending on a conditionAngular Firestore 根据条件更改此查询
【发布时间】:2021-01-06 13:05:51
【问题描述】:

如何根据条件更改此查询

 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)
}

但是,我该如何进行多重查询呢?

请帮助我。谢谢

【问题讨论】:

    标签: javascript angular firebase google-cloud-firestore


    【解决方案1】:

    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;
    

    【讨论】:

      猜你喜欢
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-23
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多