我有問題。我的應用程式允許用戶通過幾個引數過濾優惠。我想.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();
uj5u.com熱心網友回復:
查詢物件是不可變的。每次呼叫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();
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/403672.html
標籤:
