Firestore似乎无法离线工作

时间:2018-10-23 08:05:01

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

我正在尝试使用Firestore创建一个渐进式Web应用程序。当计算机处于联机状态时,Web应用程序可以正常运行,而在脱机状态下则无法运行。

我已启用如下所示的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.

我了解这两个错误在技术上都是正确的。但是,由于启用了离线持久性功能,这些错误不应该不会发生吗?

1 个答案:

答案 0 :(得分:1)

  

获取离线数据:如果您在设备离线时获取文档,   Cloud Firestore从缓存返回数据。如果没有缓存   包含该文档的数据,或者该文档不存在,   获取呼叫返回错误。

     

查询离线数据:查询具有离线持久性。您可以   使用直接get或by检索查询结果   聆听,如前面各节所述。您也可以创建   设备离线时,对本地持久数据的新查询,但是   查询最初将仅针对缓存的文档运行。

离线持久性并不意味着您的客户可以在没有Internet的情况下与Firestore进行通信,这意味着,例如,一旦再次建立Internet连接,他所做的更改便会提交给相关文档。

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

More info about using it Offline

相关问题