无法到达 Cloud Firestore 后端 - React Native Firebase v9

问题描述 投票:0回答:5

我在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...

Package.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",

身份验证和消息传递似乎工作正常,但我认为来自 firestore 的

db
存在此问题。 以下是我的
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 };

到目前为止我尝试过的

  1. 更改本答案中提到的 firestore 数据库规则:https://stackoverflow.com/a/70980383/11685381

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

  3. 根据此答案,我没有使用任何

    .env
    变量:https://stackoverflow.com/a/69095824/11685381

  4. 擦除android模拟器数据,然后冷启动。没成功。

  5. firebase-9.6.3
    升级为
    firebase-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.
javascript firebase react-native google-cloud-firestore react-native-firebase
5个回答
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};

我必须如上所述更改

db
的初始化,希望它对遇到这个奇怪错误的每个人都有效。

谢谢大家的帮助!


4
投票

这件事也发生在我身上。我在互联网上找到了很多解决方案,以及所有 6 个解决方案。注意到有帮助。我在我的 firestore 配置中使用 donenv 文件,然后我摆脱了 donenv 并将变量写入硬编码,然后它开始工作。


1
投票

首先在 firestore.js 中配置你的数据库

import firebase, { initializeApp, getApps, getApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";

let app;

if (getApps().length === 0) {
  app = initializeApp(firebaseConfig);
} else {
  app = getApp();
}

const db = getFirestore(app);


export { db, auth };

然后通过以下方式将集合添加到 fireStore DB:

import {db} from "../firebase.js";
import { collection, addDoc } from "firebase/firestore";

  const docRef = await addDoc(collection(db, "chats"), {
    chatName: input
  }).then(() => {
    navigation.goBack();
  }).catch((error) => alert(error) );

供参考:https://firebase.google.com/docs/firestore/quickstart


1
投票

2023 年编辑版本:我询问了聊天 GPT,这种格式有效。完成这些更改后重新启动服务器

import firebase from "firebase/compat/app";
import "firebase/compat/auth";
import "firebase/compat/firestore";

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

if (!firebase.apps.length) {
  firebase.initializeApp(firebaseConfig);
}

const db = firebase.firestore();
const settings = { timestampsInSnapshots: true };
db.settings(settings);

// Enable experimentalForceLongPolling for Firestore
const firestoreConfig = {
  experimentalForceLongPolling: true,
};
const firestore = firebase.firestore(firebase.app());
firestore.settings(firestoreConfig);

export { firebase, db };

0
投票

también estoy Experimentando este error

const db = 初始化Firestore(app, { 实验性的ForceLongPolling:true, });

la forma de iniciar db lo usas alone para el desarrollo o también lo dejaste para producción?

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.