javascript - 查询字符串到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;
}
这样没有错误但对我不起作用,它不会带来数据
解决方案
尝试这个。
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;
}
推荐阅读
- blocking - 写入时钟块之前的 SystemVerilog 计算
- haskell - 为什么 Haskell 中的英镑符号 '£' 显示为 '\163' 而不是输出 '£'?
- typescript - 如何在打字稿中对 cron 表达式进行排序?
- css - 使用 flexGrow 使 Material UI Grid 容器增长
- excel - Excel 宏 - 复制行 -> 在下方插入行 -> 粘贴数据
- google-api - Google gapi.auth2.getAuthInstance().isSignedIn().get() 在多个 google 帐户时始终为 false
- c++ - 数组大小超过 CPP 中的最大对象大小
- python - 尝试对字典中具有某些相似性的不同值进行分组
- swift - 如何使用 GameKit 将默认值设为 0
- ssh - 如何将已知指纹传递给 SSH(为了避免手动提示)