Router + vuex/vuex-persistedstate giving UnhandledPromiseRejectionWarning?



  • I’m currently trying to clear some console bugs. One that is getting away from me: whenever I navigate to a requiresAuth page while not logged in, or whenever I navigate to the home page while logged in (I’ve set it to bounce away from home page when logged in), I get an UnhandledPromiseRejectionWarning. I’ve traced it to the Router. I’m running SSR app, and the error is happening on the server-side console.

    Heres’s the router/index.js:

    import Vue from 'vue'
    import VueRouter from 'vue-router'
    
    import routes from './routes'
    
    Vue.use(VueRouter)
    
    /*
     * If not building with SSR mode, you can
     * directly export the Router instantiation
     */
    
    export default function ({ store, ssrContext }) {
      const Router = new VueRouter({
        scrollBehavior: () => ({ x: 0, y: 0 }),
        routes,
    
        // Leave these as is and change from quasar.conf.js instead!
        // quasar.conf.js -> build -> vueRouterMode
        // quasar.conf.js -> build -> publicPath
        mode: process.env.VUE_ROUTER_MODE,
        base: process.env.VUE_ROUTER_BASE
      })
    
      Router.beforeEach((to, from, next) => {
        
        const currentUser = store.state.auth.user
        const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
    
        /* Something about this code below is creating a "UnhandledPromiseRejectionWarning"
        When I run either the game pages while not logged in or the splash page while logged in (essentially requiring redirection), the error triggers.
        */
    
        if (requiresAuth && !currentUser) next("/")
        else if (to.path == "/" && currentUser) next("/home/main")
        else next()
    
    
      });
    
      return Router
    }
    
    

    The exact error I’m getting is this:

    (node:6944) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the
    CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

    I’m not really seeing a promise? This is likely my own oversight. Does anyone have any advice/feedback? Could this be something happening inside of the vuex-persistedstate boot file?

    Thanks, and HAPPY HOLIDAYS EVERYONE!


Log in to reply