繁体   English   中英

Firebase 9(模块化)上的复合查询

[英]Compound query on Firebase 9 (modular)

我有一个 Expo 项目,我需要在其中执行此查询:

const queryEvents = query(
    colRef,
    where('neighborhood', 'in', neighbors),
    where('curfew', '<', end)
);

其中 'neighbors' 是 geohash 数组,而 'end' 是时间戳。 到目前为止,在第一个条件下它工作正常,但每当我输入第二个条件时,firebase 都会抛出这个错误:

@firebase/firestore:, Firestore (9.9.2): 快照监听器中的未捕获错误:, {"code":"failed-precondition","name":"FirebaseError"}

对于我所阅读的内容,使用不同的运算符查询不同的字段存在一些限制,但是在文档中,有一个“有效范围”查询的示例,但这样的示例只是两个不同的查询,因为我对firestore/firebase 所以我想知道 'q1' 和 'q2' 是否可以在我的 onSnapshot 调用中使用。 这是给定的示例:

import { query, where } from "firebase/firestore";  

const q1 = query(citiesRef, where("state", ">=", "CA"), where("state", "<=", "IN"));
const q2 = query(citiesRef, where("state", "==", "CA"), where("population", ">", 1000000));

根据评论建议,这是快照:

    const getEvents = async (dbx) => {
    
        let end = new Date(Date.now() + ( 3600 * 1000 * 25 ));
        console.log("Date: ", end)
        const colRef = collection(dbx, 'events');
        const queryEvents = query(colRef, where('neighborhood', 'in', neighbors));
        onSnapshot(queryEvents, 
                (snapshot)=>{
                    let evs = [];
                    snapshot.docs.forEach((doc)=>{
                        evs.push({...doc.data(), key: doc.id})
                    })
                    setEvents(evs)
                    return evs;
                });
      }

Per another contributor's request, here's the package.json:

{
  "name": "cartelera",
  "version": "1.0.0",
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "@react-native-async-storage/async-storage": "~1.17.3",
    "@react-native-firebase/app": "^15.3.0",
    "@react-native-firebase/firestore": "^15.3.0",
    "@react-native-firebase/storage": "^15.3.0",
    "@react-navigation/bottom-tabs": "^6.3.2",
    "@react-navigation/drawer": "^6.4.3",
    "@react-navigation/native": "^6.0.11",
    "@react-navigation/native-stack": "^6.7.0",
    "expo": "~45.0.0",
    "expo-location": "~14.2.2",
    "expo-status-bar": "~1.3.0",
    "firebase": "^9.9.2",
    "formik": "^2.2.9",
    "geofire": "^6.0.0",
    "geohashes-near": "^2.0.1",
    "moment": "^2.29.4",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "react-native": "0.68.2",
    "react-native-elements": "^3.4.2",
    "react-native-gesture-handler": "~2.2.1",
    "react-native-reanimated": "~2.8.0",
    "react-native-safe-area-context": "4.2.4",
    "react-native-screens": "~3.11.1",
    "react-native-vector-icons": ">= 6.x.x",
    "react-native-web": "0.17.7"
  },
  "devDependencies": {
    "@babel/core": "^7.18.10"
  },
  "private": true,
  "react-native-dynamic-vector-icons": "WrathChaos/react-native-dynamic-vector-icons#expo"

}

事实证明,复合索引错误确实是问题所在,我没有获得设置它的链接。 我确实联系了 Google 的支持人员,但他们也没有得到答复。 我求助于卸载并重新安装 Firestore,但直到我卸载并重新安装 Firebase 后才奏效,但是,如果我只执行

%yarn add firebase

%npm install firebase

还不够,我不得不表达地使用

% npm install firebase@9.6.11

完成后,我重新启动了项目,出现了构建复合索引的链接,现在一切正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM