我正在制作一個過濾器,以便用戶可以搜索恰好幾個引數,而不必將它們全部放入表單中。例如,您可以搜索 X 型別但所有型別的游戲(將表單留空,它是一個空陣列)。
我曾嘗試同時進行整個查詢,但由于存在空陣列,因此會引發例外。因此,我想到了一步一步檢查過濾器引數并丟棄空的引數,但我不知道如何將每個步驟保存在參考中。
技術:
- 離子
- 角
- Firebase - Firestore
服務代碼:
getFilteredResultsGames(filter: Filter) {
return this.firestore.collection<Game[]>(environment.db_tables.games,
(ref) => {
if (filter.types.length > 0){
ref.where('id_type', 'in', filter.types)
}
if (filter.genders.length > 0) {
ref.where('ids_genders', 'array-contains-any', filter.genders)
}
return ref
}).valueChanges({ idField: 'id' });
}
型號代碼:
export interface Filter {
genders: string[];
types: string[];
}
uj5u.com熱心網友回復:
Firestore 查詢物件是不可變的。呼叫whereref 或查詢不會修改現有物件,而是回傳帶有附加條件的新查詢。
因此,要回傳新查詢,您需要執行以下操作:
getFilteredResultsGames(filter: Filter) {
return this.firestore.collection<Game[]>(environment.db_tables.games,
(ref) => {
if (filter.types.length > 0){
ref = ref.where('id_type', 'in', filter.types)
}
if (filter.genders.length > 0) {
ref = ref.where('ids_genders', 'array-contains-any', filter.genders)
}
return ref
}).valueChanges({ idField: 'id' });
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/460324.html
標籤:javascript 有角度的 火力基地 离子框架 谷歌云火库
上一篇:如何從離子中的打字稿方法呼叫外部javascript檔案?
下一篇:使用訂閱的值會弄亂UI
