【问题标题】:How to detach Firestore listener in from my vuex action?如何从我的 vuex 操作中分离 Firestore 侦听器?
【发布时间】:2019-02-03 18:48:14
【问题描述】:

我正在使用 Firestore 在我的 vuex 存储中获取我保存的数据。例如,当我退出时,我不太明白如何分离侦听器。

fetchExercises ({commit, getters}) {
  const db = firebase.firestore()
  const exercises = db.collection('exercises').where('userId', '==', getters.user.uid)

  // Getting Real time feeds
  exercises.onSnapshot(querySnapshot => {
    commit('setLoading', true)
    let source = querySnapshot.metadata.hasPendingWrites ? 'Local' : 'Server'

    console.log(`Source ${source}`)
    if (querySnapshot) {
      querySnapshot.forEach(doc => {
        commit('watchExercises', {
          title: doc.data().title,
          language: doc.data().language,
          translated: doc.data().translated,
          lastOpen: doc.data().lastOpen,
          dueDate: doc.data().dueDate,
          uid: doc.data().uid,
          userId: doc.data().userId,
          words: doc.data().words,
          shared: false
        })
        const exercise = {
          title: doc.data().title,
          uid: doc.data().uid,
          lastOpen: doc.data().lastOpen,
          language: doc.data().language,
          translated: doc.data().translated,
          dueDate: doc.data().dueDate,
          words: doc.data().words,
          shared: false
        }
        commit('updateExercises', exercise)
      })
    }
    if (querySnapshot.size === getters.loadedExercises.length) {
      commit('setLoading', false)
    }
  })
}

这是获取数据的操作,所以现在我想知道如何在另一个操作中分离侦听器?比如退出操作。

【问题讨论】:

    标签: javascript firebase vue.js google-cloud-firestore vuex


    【解决方案1】:
    this.unsubscribe = this.exercises.onSnapshot(
        // your code
    );
    
    // Stop listening to changes
    // Call this when a user is signed out for example
    
    this.unsubscribe();
    

    More info About it

    【讨论】:

      猜你喜欢
      • 2018-11-15
      • 2021-07-12
      • 2021-04-09
      • 2020-01-31
      • 2018-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      相关资源
      最近更新 更多