我按照文档中的说明离线激活:
firebase
.firestore()
.enablePersistence()
.then(() => {
console.log('offlinemode acctivated')
})
日志如我所料出现。
像这样添加数据时:
db
.collection('foo')
.add({foo: 'bar'})
.then(docRef => {
console.log('Added Foo: ', docRef.id)
// do some stuff here with the newly created foo and it's id.
})
.catch(console.error)
.then()
和 .catch()
都不会在离线时被调用。即使在执行此回调时对象已添加到我的离线数据库中的 foo 集合中也是如此:
db
.collection('foo')
.onSnapshot(callback)
我错过了什么吗?我希望 promise 要么失败要么解决,因此我可以做出相应的 react 。
最佳答案
只有当服务器确认写入已完成时,Firestore 中写入操作的 promise 才会解决,即使它们可能已成功写入本地缓存。
关于javascript - Firebase Firestore : when do promises from offline write operations resolve?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49818095/