Problem when trying to access data from vuex

39 Views Asked by At

I tried to put my water data in Vuex and everything went ok until I try to access my data array.

const store = new Vuex.Store({
    state: {
        categories: {}
    },
    getters: {
        categories: (state) => {
            console.log(state.categories);
            return state.categories;
        }
    },
    mutations: {
        set_categories: (state, data) => {
            state.categories = data
        }
    },
    actions: {
        get_categories: (context) => {
            axios.get(`${baseUrl}/api/categories?pagination=0`)
                .then((response) => {
                    context.commit('set_categories', response.data);
                });
        }
    }
});

Here is all the code in vista and that's how I access it in the vista component

mounted() {

        this.$store.dispatch('get_categories'); 
    },
computed: {
        stateCategories() { 
            return this.$store.state.categories 
        }
    },
console.log(this.stateCategories)

But my data does not appear. Do you know what the problem is? I tested with vue tools developers and my data appears in vuex

1

There are 1 best solutions below

2
Nikola Pavicevic On

Try to wait for response :

async mounted() {
    await this.$store.dispatch('get_categories'); 
},