首页 > 解决方案 > React Native 无法访问 Cloud Firestore 后端

问题描述

我以前使用的是 firebase 实时数据库,但现在想切换到 Cloud Firestore,但即使经过身份验证,也会出现以下错误。我目前正在使用 Android Simulator,尝试禁用我的实时数据库但找不到解决方案。

Firebase v5.4.2

[2018-09-02T12:53:42.064Z] @firebase/firestore:', 'Firestore (5.0.4): 无法访问 Cloud Firestore 后端。后端在 10 秒内没有响应。这通常表明您的设备目前没有健康的互联网连接。客户端将在离线模式下运行,直到它能够成功连接到后端。

我的配置已设置:

{
  "apiKey": "apiKey",
  "authDomain": "authDomain.firebaseapp.com",
  "databaseURL": "https://databaseURL.firebaseio.com",
  "projectId": "projectID",
  "storageBucket": "storageBucket.appspot.com",
  "messagingSenderId": "messagingSenderId"
}

根据文档,我正在添加用户集合。请注意,即使我针对数据库手动设置了用户集合,我也不会进入 .then 或 .catch 语句。

onTestPress() {
    console.log("onTestPress");
    var db = firebase.firestore();
    //console.log(db);
    const settings = { timestampsInSnapshots: true };
    db.settings(settings);

    db.collection("users").add({
      first: "Ada",
      last: "Lovelace"
    }).then(function (docRef) {
      console.log("adding document: ", docRef.id);
    }).catch(function (error) {
      console.log("Error adding document: ", error);
    });

    db.collection("users").get().then((querySnapshot) => {
      console.log(`querySnapshot: ${querySnapshot}`)
      querySnapshot.forEach((doc) => {
        console.log(`${doc.id} => ${JSON.stringify(doc.data())}`);
      });
    });
  }

这是来自 Android Logcat,您可以看到 .get() 查询返回本地副本。唯一可疑的是重复的图层名称?

09-02 14:05:37.811 2987-4016/com.myApp I/ReactNativeJS: onTestPress
    09-02 14:05:47.837 2987-4016/com.myApp E/ReactNativeJS: '[2018-09-02T13:05:47.837Z]  @firebase/firestore:', 'Firestore (5.0.4): Could not reach Cloud Firestore backend. Backend didn\'t respond within 10 seconds.\nThis 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.'
    09-02 14:05:47.849 2987-4016/com.myApp I/ReactNativeJS: querySnapshot: [object Object]
    09-02 14:05:47.849 2987-4016/com.myApp I/ReactNativeJS: 3jifIc5kyEkGU4Bzvau9 => {"first":"Ada","last":"Lovelace"}
    09-02 14:05:47.858 1431-1431/? D/SurfaceFlinger: duplicate layer name: changing com.myApp/com.myApp.MainActivity to com.myApp/com.myApp.MainActivity#1
    09-02 14:05:47.931 2987-3021/com.myApp D/EGL_emulation: eglMakeCurrent: 0x9d7857e0: ver 3 0 (tinfo 0x9d783540)

这是我的导入:

import firebase from "firebase";
import '@firebase/firestore'

应该允许读写的规则设置?

// Allow read/write access to all users under any conditions
// Warning: **NEVER** use this rule set in production; it allows
// anyone to overwrite your entire database.
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

我真的不确定我还能检查什么,所以任何建议将不胜感激!

标签: javascriptfirebasereact-nativegoogle-cloud-firestore

解决方案


我也有这个问题。警告应在 10 秒后出现。但就我而言,它出现在我加载组件时,没有超时。因为我的笔记本电脑的时间不正确。我做了正确的时间,但仍然有这个问题。所以我不得不在Windows的“日期和时间”设置中打开“自动设置时间”和“自动设置时区”。一切都对我有用。


推荐阅读