无法访问 Cloud Firestore 后端 - React Native Firebase v9

dev*_*1ce 14 javascript firebase react-native google-cloud-firestore react-native-firebase

我在 SO 上看到了很多关于这个问题的问题,但没有一个得到解答(或者解决方案不起作用),我不知道为什么。人们不断遇到这个错误,但没有提供解决方案。从过去的几天来看,即使我遇到了这个错误(注意:它似乎在我的物理设备上工作正常(不是在调试时,它只有在我释放它时才工作),但在 android 模拟器上却不起作用,所以我很确定我的互联网工作正常):

Setting a timer for a long period of time, i.e. multiple minutes, is a performance and correctness issue on Android as it keeps the timer module awake, and timers can only be called when the app is in the foreground. See https://github.com/facebook/react-native/issues/12981 for more info.
(Saw setTimeout with duration 3052257ms)
Authorization status: 1

[2022-02-09T07:05:26.500Z]  @firebase/firestore: Firestore (9.6.5): Could not reach Cloud Firestore backend. Backend 
didn't respond within 10 seconds.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will 
operate in offline mode until it is able to successfully connect to the backend.
Authorization status: 1

[2022-02-09T07:08:44.688Z]  @firebase/firestore: Firestore (9.6.5): Connection WebChannel transport errored: me {
  "defaultPrevented": false,
  "g": Y {
    "A": true,
    "J": null,
    "R": [Circular],
    "g": $d {
      "$": true,
      "$a": 2,
      "A": 3,
      "B": null,
      "Ba": 12,
      "C": 0,
      "D": "gsessionid",
      "Da": false,
      "Ea": Dd {
        "g": Cd {},
      },
      ... & so on...
Run Code Online (Sandbox Code Playgroud)

包.json:

"@react-native-firebase/app": "^14.3.1",
"@react-native-firebase/messaging": "^14.3.1",
"@react-native-google-signin/google-signin": "^7.0.4",
"expo": "~44.0.0",
"firebase": "^9.6.3",
Run Code Online (Sandbox Code Playgroud)

身份验证和消息传递似乎工作正常,但我认为来自dbfirestore 存在此问题。以下是我的配置文件中的一些代码firebase.tsx

import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getFirestore } from "firebase/firestore";

const firebaseConfig = {
  apiKey: "",
  authDomain: "",
  databaseURL: "",
  projectId: "",
  storageBucket: "",
  messagingSenderId: "",
  appId: "",
  measurementId: "",
};

const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getFirestore(app);

export { auth, db };
Run Code Online (Sandbox Code Playgroud)

到目前为止我尝试过的

  1. 如本答案中所述更改 firestore 数据库规则:/sf/answers/4968626841/

  2. 使用此解决方法:https://github.com/firebase/firebase-js-sdk/issues/5667#issuecomment-952079600

  3. .env根据这个答案,我没有使用任何变量: /sf/answers/4836707711/

  4. 擦除 Android 模拟器数据,然后冷启动。没用。

  5. 升级firebase-9.6.3firebase-9.6.6. 没用。

  6. 清理了构建文件夹,yarn 再次安装了所有包。没用。

任何人都可以为这个问题提供任何可行的解决方案吗?谢谢你!

编辑1

我特别调用 firestore db 的代码:

在我的 HomeScreen.tsx 中:

import { doc, setDoc, onSnapshot, collection } from "firebase/firestore";
import { db } from "../../../firebase";

// inside functional component
useEffect(() => {
    const addUser = () => {
      const path = doc(db, "Users", user.uid);
      const data = {
        _id: user.uid,
        name: user.displayName,
        email: user.email,
      };
      setDoc(path, data)
        .then(() => {
          console.log("User added to db");
        })
        .catch((err) => {
          console.log("Error while adding user to firestore: ", err);
        });
    };
    addUser();
  }, []);

useEffect(() => {
    const unsub = onSnapshot(collection(db, "Items"), (snapshot) =>
      setItems(snapshot.docs.map((doc) => doc.data()))
    );
    return () => unsub();
  }, []);

// so nothing is being displayed here as firestore is not working.
Run Code Online (Sandbox Code Playgroud)

dev*_*1ce 39

经过大量搜索后,我自己找到了解决方案。尽管我必须从头开始创建一个新的裸 React Native 项目,即使在那时我也遇到了这个错误,但那时我确实对 Firebase 失去了希望。然后过了一段时间,我将firebase配置更改为以下代码并且它起作用了:

import {initializeApp} from 'firebase/app';
import {getAuth} from 'firebase/auth';
import {initializeFirestore} from 'firebase/firestore';

const firebaseConfig = {
  apiKey: '',
  authDomain: '',
  projectId: '',
  storageBucket: '',
  messagingSenderId: '',
  appId: '',
  measurementId: '',
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = initializeFirestore(app, {
  experimentalForceLongPolling: true,
});

export {auth, db};
Run Code Online (Sandbox Code Playgroud)

我必须改变上面提到的初始化db,希望它对每个遇到这个奇怪错误的人都有效。

感谢大家的帮助!

  • 尝试过这个,但出现了一个奇怪的错误。`FirebaseError:已经使用不同的选项调用了initializeFirestore()。为了避免此错误,请使用与最初调用时相同的选项来调用initializeFirestore(),或者调用getFirestore()来返回已经初始化的实例。 (4认同)
  • @KyleJ 尝试重新启动服务器,这对我有帮助。 (2认同)