javascript - 如何从我的 vuex 操作中分离 Firestore 监听器?

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

我正在使用 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)
    }
  })
}

这是获取数据的操作,所以现在我想知道如何在另一个操作中分离监听器?就像注销操作一样。

最佳答案

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

关于javascript - 如何从我的 vuex 操作中分离 Firestore 监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52069664/

相关文章:

javascript - 在javascript中解压缩gzip和zlib字符串

javascript - jQuery DataTables <tbody> 未出现

javascript - touchslider.com 如何添加几个选项

android - Firestore 任务 whenallcomplete() - 任务尚未完成

vue.js - "@content": expected "}", 之后无效的 CSS 为“($material-light);

javascript - 将 JSON 数据读入 Javascript

android - 所有 firebase 依赖项的版本都是相同的,应用程序仍然会在启动后立即崩溃

ios - 为什么在 Firebase removeValue() 中删除数据不起作用?

javascript - Vuetify 自定义时间选择器组件未更新模型并给出错误

javascript - 在 Vuejs 中读取 HTML 响应并将其显示在对话框中