Alexis Gatuingt
Alexis Gatuingt

Reputation: 547

Vue Router & Vuex : Getters are undefined on page refresh

I want to create a middleware according to the tickets present in the getters of a store

I first initialize Vuejs with a query to generate my state

TicketStore.js



const getters = {
    getTickets: state => state.tickets,
    getTicketById: (state) => (id) => {
        return state.tickets.find(ticket => ticket.id === id)
    },

    //get nb of ticket for tabs
    nbTicket: state => state.tickets.length
};


const actions = { 

   .... ,

getTicketsFromAPI({commit}) {
        axios.get(api.get, {
            headers: {
                'Access-Control-Allow-Origin': '*'
            }
        })
            .then((response) => {
                response.data.data.forEach(function (el) {

                    let dateFormat = require('dateformat')
                    let now = new Date(el.created_at)

                    commit('NEW_TICKET', {
                        id: el.id,
                        name: el.name,
                        msg: el.body,
                        date: dateFormat(now, "dd/mm/yyyy à H:MM:ss")
                    })
                })
            })
}

Init in main.js

import Vue from 'vue'
import vuetify from './plugins/vuetify'
import routes from './routes/router.js'
import App from './pages/layout/App'
import TicketStore from '@/store/TicketStore'

Vue.config.productionTip = false

new Vue({
    vuetify,
    created() {
        TicketStore.dispatch('getTicketsFromAPI')
    },
    render: h => h(App),
    router: routes,
}).$mount('#app')

This way works even if I do not think it's the most suitable

Now I want to stop the navigation if the ticket does not exist; so i created a middleware in my routes.js

I first tried :

    {
        path: '/ticket/:id', component: Ticket, name: 'ticket',

        beforeEnter: (to, from, next) => {
            const id = to.params.id
            const ticket = TicketStore.getters.getTicketById(id)
            /* eslint-disable no-console */
            console.log(id, ticket, to);
            /* eslint-enable no-console */
            if (ticket && ticket.id === id) {
                next()
            } else {
                next(false)
            }
        }
    },

This way works but if I reload the page, const ticket is not defined

So I searched ; I came across multiple discussions, never really helped me, like this one Vuex store is undefined in vue-router

I try this too

        beforeEnter: (to, from, next) => {
            const id = parseInt(to.params.id)
            /* eslint-disable no-console */
            console.log(TicketStore.state)
            TicketStore.state.tickets.forEach(function (el) {
                if (el.id === id) {
                    next()
                } else {
                    next(false)
                }
            })
            /* eslint-enable no-console */
        }

Without success : this way doesnt work; but i have the ticket in my console on reload

My goal is to stop navigation if the ticket does not exist, even if the page has been reloaded

Thank you for your help

Upvotes: 1

Views: 1345

Answers (1)

Vadym Semenets
Vadym Semenets

Reputation: 143

You should define tickets as an empty array by default in store. Because for now it looks like you try get some data from store till it's not exist.

Upvotes: 1

Related Questions