Firebase 查询不适用 "where"

Firebase query not applying "where"

我正在尝试做一个多重过滤系统。所以我需要能够根据我得到的过滤器添加一个“where”。

当我尝试像这样添加“where”时,快照具有正确的房屋。

const houses = db
  .collection("Houses")
  .where("ventRent", "==", "Sell")
  .limit(paginaSize);

const snapshot = await houses.get();

但是当我尝试像这样添加“where”时,快照上有每个房子,就好像它没有添加“where”一样。

const houses = db
  .collection("Houses")
  .limit(paginaSize);

if(filters) {
  if(filters.selectCR) {
    if(filters.selectCR === "Selling") {
      houses.where("ventRent", "==", "Sell");
    } else {
      houses.where('ventRent', '==', "Rent");
    }
  }
}

const snapshot = await houses.get();

我试过改成 let、var 和 const 什么都没有。我已将 '' 更改为 "",反之亦然,但什么也没有。

我认为我的做法是正确的,但我不知道为什么不起作用

每次您调用 where 它 returns 一个新的查询。所以你必须将 houses.where(...) 的结果分配给一个变量,通常返回到 `houses.

例如:

houses = houses.where("ventRent", "==", "Sell");

然后当您调用 houses.get() 时,您是在完成查询时调用它。