TypeError:无法设置属性'单词'未定义的

时间:2018-02-13 08:21:41

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

我在vuex商店中有这个动作:

loadExercises ({commit}) {
  commit('setLoading', true)
  const db = firebase.firestore()
  db.collection('exercises').get()
    .then(querySnapshot => {
      const exercises = []
      querySnapshot.forEach((doc) => {
        exercises.push({
          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: [{ word: '', uid: '', translation: '' }]
        })
        db.collection('exercises').doc(doc.data().uid).collection('words').get()
          .then(words => {
            const wordsArray = []
            words.forEach(word => {
              wordsArray.push(word.data())
            })
            let exercise = this.getters.loadedExercise(doc.data().uid)
            exercise.words = wordsArray
          })
          .catch(error => {
            commit('setLoading', false)
            console.log(error)
          })
      })
      commit('setLoading', false)
      commit('setLoadedExercises', exercises)
    })
    .catch(error => {
      commit('setLoading', false)
      console.log(error)
    })
}

应该从firebase cloudstore db获取练习。它适用于某些路线但不是全部。

当使用这两个吸气剂时,它可以工作:

loadedExercises (state) {
  return state.loadedExercises
},
loadedExercise (state) {
  return (exerciseId) => {
    return state.loadedExercises.find(exercise => {
      return exercise.uid === exerciseId
    })
  }
}

但是当我使用这些吸气剂时:

upcomingExercises (state, getters) {
  return getters.loadedExercises.filter(exercise => {
    return exercise.dueDate > 0
  })
},
latestExercises (state, getters) {
  return getters.loadedExercises.splice(0, 5)
},

它不起作用我得到" TypeError:无法设置属性'单词'未定义"。我做错了什么?

1 个答案:

答案 0 :(得分:0)

在我看来,你不会将值返回给函数。

尝试替换

db.collection('exercises').get()

return db.collection('exercises').get()

db.collection('exercises').doc(doc.data().uid).collection('words').get()

return db.collection('exercises').doc(doc.data().uid).collection('words').get()