Firebase Cloud Firestore 抛出 "client is offline"

Firebase Cloud Firestore throws "client is offline"

加载页面的 4 次中大约有 3 次我从 firestore 客户端 SDK(网络)收到此错误。这极大地减慢了开发速度。 Auth、Realtime DB 和 Storage 工作得很好。

Failed to get document because the client is offline.

我能做些什么吗?

我遇到了一些问题。但我刚刚修好了。检查您的 projectId 是否正确。当您使用 Cloud Firestore 时,您需要它。

export const environment = {
  production: false,
  firebaseConfig: {
    apiKey: 'XYZ',
    authDomain: 'XYZ.firebaseapp.com',
    databaseURL: 'https://XYZ.firebaseio.com',
    storageBucket: 'XYZ.appspot.com',
    projectId: 'XYZ' // <--- make sure project ID is here 
  }
};

目前客户端与后端路由之间的交互导致了此问题。结果客户认为网络出现故障并离线。

我们很快就会对此进行修复。具体时间我不能保证,但这是我们要解决的首要问题之一。

感谢您的耐心等待。

我刚刚花了大约 3 个小时来修复它。我的模拟器出了点问题。我不得不删除它,然后创建新的,现在它正在工作。因此,如果您遇到此问题,请尝试一下。

我更新了我的所有软件包,并且 运行 遇到了这个问题。
我为我的 Web 应用程序启用了离线持久性并解决了问题:

firebase.firestore().enablePersistence()

https://firebase.google.com/docs/firestore/manage-data/enable-offline

对我来说,启用 ({ synchronizeTabs: true } ) 似乎有所帮助。

对我来说,同样的错误发生在

  1. project_id错了
  2. 模拟器的端口是 8081 而不是 8080。(当端口是 8081 时模拟器运行良好,UI 正常打开等等,但错误一直显示。我通过更改端口解决了这个问题回到 8080)

错误信息非常具有误导性。

可能会出现此问题,因为您在 React Native(RN CLI 而非 Expo)应用中集成了 firebase 'web' SDK。因此,为了在您的 React Native 应用程序中使用 firebase,您必须遵循本教程:https://rnfirebase.io/

一定有用。

尝试使用 VPN,它会起作用的!。 我切换到 VPN,它 100% 有效。有时 Firebase 无法响应某些国家/地区的 IP 地址。

对我来说,重启模拟器就解决了。

在我的应用程序中,此错误也发生在安装后的第一个 运行。我能够通过 refreshing/restarting Activity.

摆脱上述错误
if (e.getMessage().equals("Failed to get document because the client is offline.")){
     finish();
     startActivity(getIntent());
}

此代码必须放在addOnFailureListener

以防万一有人在网络上遇到这个问题(服务器端代码),只需尝试重新启动服务器并再次 ping 连接到 firebase 的端点。这对我有用!

在我的例子中,我使用的是 Chrome 扩展程序 Moesif CORS。我禁用了扩展程序并且它有效。

在我的例子中,我收到了这个问题以及一条警告,提示我应该更改我的数据库位置,因为它位于不同的区域。
我在 firebaseconfig 中添加了一个 databaseURL 属性 和我的 dsatabase 的 url,之后它工作正常。

我的问题是我之前使用 firebase-local-emulator 测试了我的应用程序,因此仍然启用了一些配置,例如:

Firebase.firestore.apply {
    if (DomainConstants.isOnTest) useEmulator("10.0.2.2", 8080)
}

因为我忘记设置 isOnTest = false,应用程序依赖 firebase-local-emulators 数据(不是 运行)并抛出 client is offline error

长话短说:检查您是否使用 firebase-local-emulator 配置您的项目,并在您想要使用“真实”数据库时将其关闭

在控制台中看到此错误,启用数据存储可能会有所帮助。

@firebase/firestore: Firestore (9.6.9): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=not-found]: The project my-project does not exist or it does not contain an active Cloud Datastore or Cloud Firestore database. Please visit http://console.cloud.google.com to create a project or https://console.cloud.google.com/datastore/setup?project=my-project to add a Cloud Datastore or Cloud Firestore database. Note that Cloud Datastore or Cloud Firestore always have an associated App Engine app and this app must not be disabled. 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.

就我而言,我在数据库中使用了前导斜线 URL:

admin.initializeApp({
    credential: admin.credential.cert(CREDENTIALS_PATH), 
    databaseURL: "https://databasename.firebaseio.com/"
                                      // remove this ^
});