javascript - Firestore 无法工作 似乎可以离线工作

标签 javascript firebase google-cloud-firestore progressive-web-apps

我正在尝试使用 firestore 创建一个渐进式网络应用程序。当计算机在线时,网络应用程序可以正常工作,但在离线时则无法工作。

我已经启用了 firestore 离线持久性,如下所示-

firebase.firestore().settings({ timestampsInSnapshots: true });
firebase.firestore().enablePersistence().then(() => {
    this.db = firebase.firestore();
});

如果我在计算机离线时运行该应用程序,则会收到以下错误。

[2018-10-23T07:15:24.406Z]  @firebase/firestore: Firestore (5.5.0): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=unavailable]: The operation could not be completed
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.

尝试从 firestore 数据库获取文档失败并出现错误

Uncaught (in promise) Error: Failed to get document because the client is offline.

我知道这两个错误在技术上都是正确的。但这些错误不是应该因为我启用了离线持久化而不会发生吗?

最佳答案

Get offline data: If you get a document while the device is offline, Cloud Firestore returns data from the cache. If the cache does not contain data for that document, or the document does not exist, the get call returns an error.

Query offline data: Querying works with offline persistence. You can retrieve the results of queries with either a direct get or by listening, as described in the preceding sections. You can also create new queries on locally persisted data while the device is offline, but the queries will initially run only against the cached documents.

离线持久性并不意味着您的客户可以在没有互联网的情况下与 Firestore 进行通信,这意味着一旦再次出现互联网连接,他所做的更改就会提交到相关文档。

考虑缓存相关文件,以便用户在一定程度上离线使用 PWA。

More info about using it Offline

关于javascript - Firestore 无法工作 似乎可以离线工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52944048/

相关文章:

android - Firestore 持久性如何真正发挥作用?

firebase - 在 firebase 控制台中按日期过滤 firestore 查询

javascript - 与 firebase onDisconnect react native

android - 如何使用 FCM 从 Firebase 控制台发送按钮按下时的自动消息?

ios - 从 Firebase 数据库异步方法返回值

javascript - JavaScript 中的类型错误

javascript - 通过 Markdown 解析器在 HTML 渲染期间删除第一行 block

database - 如何在 NoSQL 环境中创建以下数据结构

javascript - 使用 Ajax 加载器加载 Facebook 评论

javascript - 从同一工厂调用 Angular 工厂中的变量