IvanRadojevic
4 weeks ago
121
1
Vue

Vuex stat empty array?

Posted 4 weeks ago by IvanRadojevic

I try to load categories in state, but array is empty? In the same way I loaded user and it is working.


export default {

  state: {

    user: {},

    categories: [],
  },

  mutations: {
    setUser(state, payload) {
            state.user = payload;
    },

    setCategories(state, categories) {
            state.categories = categories;

    },
    
  },

  actions: {
    async loadUser({ commit, dispatch }) {
      if (isLoggedIn()) {
        try {
          const user = (await axios.get("/api/user")).data; 
          commit("setUser", user);  //vuex state   
          
        } catch (error) {

        }
      }
    },


    async loadCategories({ commit }) {     
        try {
          const categories = (await axios.get("/api/categories")).data; 
          commit("setCategories", categories); 

        } catch (error) {
          //console.log(error);
        }     
    },
     
  },

};

Please sign in or create an account to participate in this conversation.