首页 > 解决方案 > 查询字符串到firestore Angular - 多种选择

问题描述

我正在使用 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;
  }

这样没有错误但对我不起作用,它不会带来数据

标签: javascriptangularfirebasegoogle-cloud-firestore

解决方案


尝试这个。

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

推荐阅读