首页 > 解决方案 > 有 where 子句时 React Native Firebase 异步函数退出

问题描述

这有效

  const getCatListings = async () => {
    setIsLoading(true);
    let snapshot = await listingsRef.orderBy('expires').limit(20).get();

    if (!snapshot.empty) {
      let newListings = [];

      setLastDoc(snapshot.docs[snapshot.docs.length - 1]);

      for (let i = 0; i < snapshot.docs.length; i++) {
        newListings.push(snapshot.docs[i].data());
      }

      setData(newListings);
    } else {
      setLastDoc(null);
    }

    setIsLoading(false);
  }

但是当我添加它时,它会在没有抛出任何错误来解释原因的情况下.where("categoryID", "==", category.id)退出函数。setIsLoading(true)

let snapshot = await listingsRef.where("categoryID", "==", category.id).orderBy('expires').limit(20).get();

我读到“==”不需要 categoryID 上的 orderBy,但都试过了。任何帮助表示赞赏。

标签: firebasereact-nativegoogle-cloud-firestorepaginationinfinite-scroll

解决方案


当您在单个 firebase 查询中同时拥有 where 和 orderby 时,您需要在 firebase 中添加一个索引来支持此查询。

您可以在此处的官方文档中阅读它。


推荐阅读