【问题标题】:Query string to firestore Angular - multiple choices查询字符串到firestore Angular - 多种选择
【发布时间】:2021-01-05 23:29:51
【问题描述】:

我正在使用 Firestore 数据库在 Angular 中创建一个应用程序。 我必须使用不同的选择参数进行查询。

如何创建一个字符串,以便查询根据用户的选择而变化?

应用程序有一个包含以下字段的选择表单:

Date from
Date Until
status
Client
Seller
paymenttype

有多种可能的组合,想法是用已选择的字段构建一个字符串

例如:

在.ts中

if (typeof this.staTus != "undefined" && typeof this.paymenttype != "undefined" && typeof this.Client != "undefined" && typeof this.Seller != "undefined") {

  strquery = '"order", ref => ref.where("DateOrder", ">=", "' + this.Datefrom + '").where("DateOrder", "<=", "' + this.DateUntil + '").where("idclient", "==", "' + this.Client + '").where("status", "==", "' + this.staTus + '").where("seller", "==", "' + this.seller + '").where("paymenttype", "in", "' + this.paymenttype + '").orderBy("DateOrder", "desc").limit(5000)';

  this.orderServices.getOrdersRep(strquery).subscribe(ord => {
    this.Ped_ = ord;
  })
}

在服务中

  getOrdersRep(strq?) {
    this.ordersColletionrep = this.db.collection(strq);

    this.ordersrep = this.ordersColletionrep.snapshotChanges().pipe(map(changes => {
      return changes.map(a => {
        const data = a.payload.doc.data() as Order;
        return data;
      })
    }));
    return this.ordersrep;
  }

这种方式没有错误但对我不起作用,它不带数据

【问题讨论】:

    标签: javascript angular firebase google-cloud-firestore


    【解决方案1】:

    试试这个。

    ts

    if (typeof this.staTus != "undefined" && typeof this.paymenttype != "undefined" && typeof this.Client != "undefined" && typeof this.Seller != "undefined") {
    
      const query = (ref: CollectionReference) => {
        return ref.where("DateOrder", ">=", this.Datefrom )
          .where("DateOrder", "<=", this.DateUntil)
          .where("idclient", "==", this.Client)
          .where("status", "==", this.staTus)
          .where("seller", "==", this.seller)
          .where("paymenttype", "in", this.paymenttype)
          .orderBy("DateOrder", "desc")
          .limit(5000);
      };
    
      this.orderServices.getOrdersRep(query).subscribe(ord => {
        this.Ped_ = ord;
      })
    }
    

    服务

    getOrdersRep(query) {
      this.ordersColletionrep = this.db.collection("order", query);
    
      this.ordersrep = this.ordersColletionrep.snapshotChanges().pipe(map(changes => {
        return changes.map(a => {
          const data = a.payload.doc.data() as Order;
          return data;
        })
      }));
      return this.ordersrep;
    }
    

    【讨论】:

    • 很好,它对我有用,但我需要根据选择更改查询。你能帮我解决这个问题吗?
    • 我猜你还需要修改你的html。请提供您的 html。
    猜你喜欢
    • 2014-09-27
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多