A
A
Artemio952020-09-26 14:54:54
Vue.js
Artemio95, 2020-09-26 14:54:54

Why does nothing come to state?

Hello! Can't figure it out... Here is the code

export const state = () => ({
  places: []
})

export const mutations = {
  SET_PLACES_TO_STATE: (state, payload) => {
    state.places = payload;
  },
}

export const actions = {

  GETPLACES({ commit }) {

    let promise = new Promise(resolve => {

      let getPlaces = this.$fireDb.ref()
      const receivedPlacesArray = []

      getPlaces.on(
        'value',
        (snapshot) => {
          const receivedPlaces = snapshot.val()
          receivedPlaces.forEach((el) => {
            let item = {
              id: el.id,
              status: el.status,
            }
            receivedPlacesArray.push(item)
          })
          resolve(receivedPlacesArray)
        },
        (error) => {
          console.log('Error: ' + error.code)
        }
      )
    })
    promise.then(receivedPlacesArray =>{
      console.log(receivedPlacesArray)
      commit('SET_PLACES_TO_STATE', receivedPlacesArray);
    })

  }
}
export const getters = {
  GET_PLACES_FROM_STATE(state) {
    return state.places
  }
}


If I dispatch an action in a component, the data array is output to the console. I can put the resulting array into the component's local variable and everything is ok. But I want to transfer it to the state and already pull the components from there. What could be wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
YoloV, 2020-09-26
@YoloV

SET_PLACES_TO_STATE: (state, payload) => {
    state.places.push(payload)
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question