React Native无法到达Cloud Firestore后端

时间:2018-09-02 13:29:12

标签: firebase react-native google-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;
    }
  }
}

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

1 个答案:

答案 0 :(得分:0)

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