Home » vue » VueRouter's beforeEach causes error: failed to convert exception to string

VueRouter's beforeEach causes error: failed to convert exception to string

Posted by: admin November 26, 2021 Leave a comment

Questions:

My console says:

[vue-router] uncaught error during route navigation:
<failed to convert exception to string>

The line that causes that error is the one in main.js:

next('/login')

my main.js file:

import Vue from 'vue'
import VueRouter from 'vue-router'
import Vuex from 'vuex'
import App from './App.vue'
import Routes from './routes'

import { store } from './store/store';


Vue.use(VueRouter);

const router = new VueRouter({
    routes: Routes
})

router.beforeEach((to, from, next) => {
    next('/login');
})

var vm = new Vue({
  el: '#app',
  store: store,
  router: router,
  render: h => h(App),
})

My routes.js file:

export default [
    { path: '/', component: Game, cors: true },
    { path: '/login', component: Login },
    { path: '/signin', component: SignIn },
    { path: '/gamerouter', component: GameRouter },
    { path: '/waitingforplayers', component: WaitingForPlayers, name: "buba" },
    { path: '/selectPlayersForTeams', component: SelectPlayersForTeams },
    { path: '/startStatistics', component: StartStatistics },
    { path: '/gamelocked', component: GameLocked },
    { path: '/answering', component: Answering },

]

I also get this error if i put next(‘/’), but i do not get this error if i write next() or next(false);
Any ideas that might help me fix this?

Answers:

next has to be called without parameters to confirm navigation, otherwise you will keep triggering the beforeEach hook:

router.beforeEach(function(to, from, next) {
  console.log(to.path)
  if (to.path !== '/timer') {
    next("/timer");
  } else {
    next();
  }
});