【问题标题】:How to enable offline data in firestore for web如何在 Firestore for Web 中启用离线数据
【发布时间】:2018-08-09 14:58:43
【问题描述】:

我想在我的项目中启用离线数据。 我找到了正确的代码,但我不知道在哪里实现代码

我在firebaseConfig.js 文件中实现代码:

import firebase from 'firebase'
import 'firebase/firestore'

// firebase init
// init code goes here
var config = {
 apiKey: '',
 authDomain: '',
 databaseURL: '',
 projectId: '',
 storageBucket: '',
 messagingSenderId: ''
} 
firebase.initializeApp(config)

firebase.firestore().enablePersistence()
 .then(function () {
   // Initialize Cloud Firestore through firebase
   var db = firebase.firestore();
 })
 .catch(function (err) {
   console.log(err)
 })

 // firebase utils
 const db = firebase.firestore()
 const oldRealTimeDb = firebase.database()
 const auth = firebase.auth()
 const currentUser = auth.currentUser

 // date issue fix according to firebase
 const settings = {
   timestampsInSnapshots: true
 }
 db.settings(settings)

 // firebase collections
 const usersCollection = db.collection('users')
 const postsCollection = db.collection('posts')

export {
 db,
 auth,
 currentUser,
 postsCollection,
 usersCollection

}

main.js

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import {store} from './store'
import './registerServiceWorker'
import Vuetify from 'vuetify'
import 'vuetify/dist/vuetify.min.css' // Ensure you are using css-loader 
const fb = require('./firebaseConfig.js')

Vue.config.productionTip = false

export const bus = new Vue()

Vue.use(Vuetify)

let app
fb.auth.onAuthStateChanged(user => {
 if (!app) {
  app = new Vue({
   el: '#app',
   store,
   router,
   template: '<App/>',
   components: {
     App
   },
   render: h => h(App)
 }).$mount('#app')
}
})

我收到了这个错误:

【问题讨论】:

  • 如果我没记错的话,Lukas,你使用的是 Vue.js。你能分享你的 main.js 文件(可能还有 main.js 中需要的所有文件)

标签: javascript firebase google-cloud-firestore


【解决方案1】:

documentation 中提供的示例代码建议您应该调用 enablePersistence() 并可能在由于某些给定原因而失败时记录:

firebase.initializeApp({
  apiKey: '### FIREBASE API KEY ###',
  authDomain: '### FIREBASE AUTH DOMAIN ###',
  projectId: '### CLOUD FIRESTORE PROJECT ID ###',
});

firebase.firestore().enablePersistence()
  .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
          // ...
      }
  });

调用enablePersistence() 之后的后续查询将在内部排队,直到完全完成,这意味着查询可能使用也可能不使用本地缓存的数据,具体取决于enablePersistence() 的结果。如果能够使用本地持久性对您的应用很重要,您可能希望在执行查询之前等待其结果(触发返回的 Promise)。

【讨论】:

  • 第一个答案在技术上并不正确,因为它忽略了从 enablePersistence 返回的承诺。它不保证下一个查询将使用持久性。使用文档中提供的示例代码以获得最佳行为。
  • 我怎么知道第一个例子不起作用?
  • 在没有互联网连接的情况下,调用 enablePersistence() 后立即发生的查询是否能够检索缓存数据?如果不是,则持久性不起作用。
  • 我刚刚与 Firestore 客户团队讨论过这个问题。似乎原始代码示例具有误导性。实际上没有必要等待 enablePersistence() 返回的承诺的结果来进行第一次查询。查询将在内部排队,直到 enablePersistence() 完全完成。文档中的代码示例将很快更新。我也会更新我的答案。
  • 你能看一下这个处理在线/离线状态的吗:stackoverflow.com/questions/51768240/…
【解决方案2】:

只需删除第二个firebase.firestore() 并调用 enablePersistence 如下:

import firebase from 'firebase'
import 'firebase/firestore'

// firebase init
// init code goes here
var config = {
 apiKey: '',
 authDomain: '',
 databaseURL: '',
 projectId: '',
 storageBucket: '',
 messagingSenderId: ''
} 
firebase.initializeApp(config)

const db = firebase.firestore();
const auth = firebase.auth();
const currentUser = auth.currentUser;

// date issue fix according to firebase
const settings = {
  timestampsInSnapshots: true
};
db.settings(settings);
db.enablePersistence();

// firebase utils
//const db = firebase.firestore() // <---- Remove this line
const oldRealTimeDb = firebase.database()
const auth = firebase.auth()
const currentUser = auth.currentUser

【讨论】:

  • 是的,这也是我以前做过的,但同样的错误
  • 我认为你应该向上移动db.settings(settings)
  • 我做了一些额外的改变。你能确认它还可以吗(我暂时无法测试)
  • 它可以工作,但不适用于 db.collection()... -> 'collection' of undefined
  • 这段代码如何可靠地工作?从 enablePersistence 返回的承诺被忽略。在 promise 成功解决之前,持久性实际上并没有启用。 firebase.google.com/docs/reference/js/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
  • 2021-11-30
  • 2021-04-09
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
相关资源
最近更新 更多