激活离线持久化后如何导出 Firestore?



我一直在ReactJS框架中使用Firebase。我已经设置了一个配置.js在其中初始化Firebase并导出我的Firestore实例,现在一切正常:

export const store = firebase.firestore();

我正在尝试启用离线持久性,但现在应该如何导出我的存储变量?

firebase.firestore().enablePersistence()
.then(function() {
// Initialize Cloud Firestore through firebase
var store = firebase.firestore();
})
.catch(function(err) {
if (err.code == 'failed-precondition') {
// Multiple tabs open, persistence can only be enabled
// in one tab at a a time.
// ...
} else if (err.code == 'unimplemented') {
// The current browser does not support all of the
// features required to enable persistence
// ...
}
});

我试图导出承诺,但它不起作用。有什么建议吗?

因为他们改变了行为并且没有正确的方法告诉我们,所以我这样做了:

消防.js:

import firebase from 'firebase'
import 'firebase/firestore'
export var db = firebase.firestore()
const settings = { timestampsInSnapshots: true }
db.settings(settings)

export function initializeDatabase() { 
return new Promise((resolve, _) => {
firebase.firestore().enablePersistence({experimentalTabSynchronization:true})
.then(function() {
//update the database variable to reflect the one with persistenceEnabled
db = firebase.firestore();
resolve()
})
.catch(function(err) {
if (err.code == 'failed-precondition') {
// Multiple tabs open, persistence can only be enabled
// in one tab at a a time.
// ...
} else if (err.code == 'unimplemented') {
// The current browser does not support all of the
// features required to enable persistence
// ...
}
//Nothing to do, it was previsoly initilaized in the var db 
resolve()
});
})
}

现在在main.js中,您应该调用初始化数据库的方法

主.js:

let app;
initializeDatabase().then(function() {
initializeSecureApp()
})
function initializeSecureApp() {
if(!app) {
app = new Vue({
router,
store,
render: h => h(App)
}).$mount('#app');
// defaultListeners()
}
}

PS:我有点菜鸟,但这是我发现仅在设置持久性数据库后启动应用程序的唯一方法

PS:对不起,如果问题很旧,但是我今天遇到了这个问题,不得不搜索它

ps:你可以实现拒绝而不是 _ 并更新对初始化数据库的调用来处理错误

如果有人有更好的方法,请在此处发布

最新更新