我有问题。我的应用程序允许用户通过几个参数筛选报价。我想用.where()
运算符来获取数据,因为我需要对它们进行堆栈。我该怎么做?
我的尝试(无效(:
let query = db.collection("cards").where("cardId", "==", id);
if (filterParams.price.from && filterParams.price.to) {
query
.where("price", ">=", filterParams.price.from)
.where("price", "<=", filterParams.price.to);
}
if (filterParams.graded) {
query.where("isGraded", "==", filterParams.graded);
}
if (filterParams.condition) {
query.where("condition", "==", filterParams.condition);
}
query = await query.get();
查询对象是不可变的。每次调用where
时,它都会返回一个新的Query
对象,然后需要保留对该查询的引用。
因此:
let query = db.collection("cards").where("cardId", "==", id);
if (filterParams.price.from && filterParams.price.to) {
query = query // 👈
.where("price", ">=", filterParams.price.from)
.where("price", "<=", filterParams.price.to);
}
if (filterParams.graded) {
query = query.where("isGraded", "==", filterParams.graded); // 👈
}
if (filterParams.condition) {
query = query.where("condition", "==", filterParams.condition); // 👈
}
query = await query.get();