我正在尝试使用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.
我了解这两个错误在技术上都是正确的。但是,由于启用了离线持久性,难道不会发生这些错误吗?
最佳答案
获取离线数据:如果您在设备离线时获取文档,
Cloud Firestore从缓存返回数据。如果没有缓存
包含该文档的数据,或者该文档不存在,
get call返回错误。
查询脱机数据:查询具有脱机持久性。您可以
使用直接get或by检索查询结果
聆听,如前面各节所述。您也可以创建
设备离线时,对本地持久数据的新查询,但是
查询最初将仅针对缓存的文档运行。
离线持久性并不意味着您的客户可以在没有互联网的情况下与Firestore进行通信,这意味着,例如,一旦再次建立互联网连接,他所做的更改便会提交给相关文档。
考虑为用户缓存相关文件以在一定程度上使用离线PWA。
More info about using it Offline
关于javascript - Firestore似乎无法离线工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52944048/