【发布时间】:2018-01-31 18:29:25
【问题描述】:
我在将 google firestore 的离线行为集成到我的聚合物应用程序中时遇到问题。
我创建了一个元素来使用 firestore 执行所有数据库操作。我已经在构造函数中初始化了连接,下面是代码。
constructor() {
super();
if (!this.db) {
firebase.firestore().enablePersistence()
.then(() => {
this.db = firebase.firestore();
})
.catch((err) => {
if (err.code == 'failed-precondition') {
} else if (err.code == 'unimplemented') {
}
});
}
}
我创建了另一个将用户详细信息保存到 Firestore 的功能。
saveUserAndSync() {
this.db.collection("users").add(this.userObj)
.then(function (docRef) {
console.log("Document written with ID: ", docRef.id);
})
.catch(function (error) {
console.error("Error adding document: ", error);
});
}
即使离线也能很好地工作。我面临的最大问题是,如果应用程序处于脱机状态并且我创建了用户,同时如果我重新加载页面,所有数据都会丢失,并且与 firestore 的同步将无法正常工作。
谁能建议解决这个问题。
我能够使用 pouchDB 和 couchDB 进行相同的实现,因为 pouchDB api 将首先将数据保存在本地,然后将其与 couchDB 同步。
但使用 firestore 会失败。
【问题讨论】:
-
大家好,只是一个小更新。如果应用程序处于脱机状态,我可以使用 enablePersistence() 使其工作。我面临的唯一问题是,如果我重新加载页面,它就无法正常工作。但是数据确实存储在 firebase 的 indexedDB 中。但是只有在启动另一个操作时才会发生同步。任何人都可以建议我在应用程序重新上线时如何同步。或者我是否缺少任何配置以使其正常工作。
标签: javascript firebase google-cloud-firestore polymer-2.x polymerfire