как получить данные с mapActions - PullRequest
0 голосов
/ 28 декабря 2018

Я не могу получить нужные мне данные, используя mapActions из моего магазина.Я делаю Axios GET (я превращаю эти данные в массив), и передаю эти данные в мой home.vue и отображаю список заметок.Теперь, если я использую mapGetters, он работает нормально, но, насколько я понимаю, я могу получить доступ к данным напрямую из mapActions, я видел, как люди это делают, но пока не могу.Или я могу?

Home.vue:

  export default {
  methods:{
    // Not Working
    ...mapActions(
        ['getNotes']
    ),
    created(){
    // Not working
    this.getNotes()
    console.log(this.getNotes())//returns pending Promise
  }
}

my store.js

export default new Vuex.Store({
  state: {
    ...other stuff in state...
    // this is getting the notes from firebase
    notes: {}
  },
  getters: {
    ...other getters...
    notes: state => state.notes
  },
  mutations: {
    ...other mutations...
    SET_NOTES (state, notes) {
      state.notes = notes
    }
  },
  actions: {
    getNotes ({ commit }) {
      axios.get('/data.json')
        .then(res => {
          const incoming = res.data
          const notes = [] // <-- this is commited ok without explicit return
          // converting object to array
          // extracting firebase ids for manipulating existing notes
          for (let key in incoming) {
            const note = incoming[key]
            note.id = key
            notes.push(note)
          }
          console.log(notes)
          commit('SET_NOTES', notes)
          // return notes <-- tried that, no effect!
        })
        .catch((error) => {
          console.log('Error: ', error)
        })
    },
    ...commiting 2 other things needed for my app
  }
  ...other actions...
})

1 Ответ

0 голосов
/ 29 декабря 2018

Я не вижу, чтобы вы возвращали данные заметок в качестве возвращаемого значения в вашем действии getNotes().В конце вашего успешного обратного вызова все, что вы сделали, это зафиксировали свои данные в заметках commit('SET_NOTES', notes).

Возврат данных заметок

getNotes ({ commit }) {
  axios.get('/data.json')
    .then(res => {
      const incoming = res.data
      const notes = []
      // converting object to array
      // extracting firebase ids for manipulating existing notes
      for (let key in incoming) {
        const note = incoming[key]
        note.id = key
        notes.push(note)
        // array.reverse()
      }
      console.log(notes)
      commit('SET_NOTES', notes)
      // HERE YOU RETURN YOUR NOTES DATA
      return notes
    })
    .catch((error) => {
      console.log('Error: ', error)
    })
}
...