Error in vue-router when button is clicked repeatedly

In my Rails app, I am working with Vue.js and vue-router. I have a button that triggers a method called navigate. When the user clicks on this button, it sends a request to an API endpoint using the axios module and then moves to the next component with

this.$router.push({name: "RouteName"})
. However, when the user clicks multiple times on this button, I encounter an error in the console saying Uncaught (in promise) undefined. My assumption is that this error is related to vue-router.

Here is part of the code from Home.vue component:

<template>
  <div>
    <button
      id="select_button"
      @click="onSelectClick"
    >
     Select
    </button>
  </div>
</template>
<script>
  export default {
    onSelectClick() {
      this.$`axios.get('some-url').then(res => { 
        //print response in console
      }).catch(err => { 
        // print error in console
      })
      this.$router.push({ name: 'route-name' }); // route-name is not home
    }
  }
</script>
<style>
</style>

Error message when double-clicking or clicking multiple times on the button:

Uncaught (in promise) undefined

Answer №1

Issue: The error is triggered by vue-router when the method

this.$router.push({name: 'route-name'})
is called repeatedly without completing the previous call.

Resolution: To solve this issue, it is recommended to include a catch guard in the router.push function.

this.$router.push({name: 'router-name'}).catch(error => { //handle error here})

To make it more generic, a method can be added to a Vue mixin.

import router from '../router'
Vue.mixin({
  methods: {
    navigate(route) {
      router.push(route).catch(err => { //handle error here });
    }
  }
});

This enables the usage of the navigate method in components as shown below.

<template>
</template>
<script>
  export default {
    mounted() {
      this.navigate({name: 'Home'})
    }
  }
</script>
<style>
</style>

Hoping that this guidance will benefit others who encounter similar errors.

Answer №2

navigateTo({page: 'Login'})

leads to Uncaught (in promise)...

After making a change to:

redirect.to({page: 'Login'})
finish

and the error is gone!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to increase the size of the icon without altering the dimensions of the button?

After implementing a code snippet to enable a button to copy the URL to the clipboard, I encountered an issue. Originally, the button displayed the text "copy," which changed to "copied" after clicking on it and reverted back after 2 seconds. However, I d ...

Having difficulty deleting an entry from a flatList in a React Native component when using the filter method

I'm currently facing an issue with deleting an item from my flatlist in React Native. I've been attempting to use the filter method to exclude the list item with the ID entered by the user for deletion, but it's not working as expected. I&ap ...

I encountered an error with status code 401 despite providing the API token as required

Can anyone help me troubleshoot an issue I'm having with a POST request using VueJS in Laravel? The request keeps failing with a 401 status code, even though I'm passing the token in the headers. const post_data = { headers: { Authoriza ...

How can I extract information from an HTML element using its ID in JavaScript?

Can anyone please help me retrieve the date using JavaScript from the following div: <div id="popupDateFieldMin" class="dateField">2017-08-02</div> Below is the code snippet I am currently using: var cal = document.getElementById( 'popu ...

Values in Local Storage are not located upon first attempt, but after a reload they function properly

import {useEffect} from 'react'; import {useRouter} from 'next/router'; const AuthenticationGuard=props=>{ const {children,fallback} = props; const auth = useAuth(); const router=useRouter(); useEffect(()=>{ if(!r ...

What is the process for importing Progressbar.js into a Laravel project?

I am attempting to implement a progress bar in Laravel 5.6 using this specific progress bar library. The Progress.js documentation indicates that the following steps are necessary: var ProgressBar = require('progressbar.js') Within the default ...

Is it possible to include more than one ng-app directive on a single HTML page in AngularJS?

When working with Angular JS, I've noticed that I only get the desired output if I remove either the ng-app directive for demo1 or the models. It seems like having two ng-app directives active at the same time causes issues. As a beginner in Angular J ...

React Nextjs implementation of a fixed navigation bar to stick at the top

Hello, I am experiencing some issues setting up a sticky navbar in Next.js. The current navbar doesn't seem to be functioning as expected. Is there anyone who can assist me with the code provided below? import React, { useEffect } from 'react&apo ...

How can I streamline a kendo UI MVC project by eliminating unnecessary components?

After switching my MVC 5 project to utilize Kendo UI, I've noticed a significant increase in the number of files being used. Since there is no need for supporting other cultures at the moment, can I confidently delete the files within the messages an ...

Creating a dynamic dropdown list with PHP and AJAX using JQuery

I was attempting to create a dynamic dependent select list using AJAX, but I am facing issues with getting the second list to populate. Below is the code I have been working with. The gethint.php file seems to be functioning properly. I'm not sure whe ...

Tips for automatically closing the Toggle Navigation feature in Vue JS when a user clicks outside of the navigation container

Is there a way to close the toggled navigation menu automatically when the user clicks outside of the navigation container? I have implemented two ul menus inside the navigation menu and would like the subNavActive, safNavAcitve, and repNavUl variables to ...

Guide on adjusting PHP variable values and updating functions with ajax requests

One of my functions determines dates based on a variable For example: $modification = "+1 Week" function updateCalendar($change){ $month = array("January","February","March","April","May","June","July","August","September","October","November","Dece ...

Trigger and maintain click action in Javascript

I am currently in the planning stages of a project, and I'm facing an issue with a click and hold event. My goal is to have a div that allows me to click through it (meaning I can still interact with elements underneath), while having an opacity of ar ...

What should you choose between vuex "store" and "data: store" in the Vue() constructor?

Vue documentation recommends using the "data" option in the constructor to maintain global or shared data: Link This approach seems logical and practical. Contrastingly, Vuex documentation initializes the "store" object without specifying a property name ...

Create a dynamic animation effect for the placeholder in an input field that triggers when the user starts typing

Have you ever come across interactive demos like this one? I've noticed that most examples involve creating a new element. parent().append("<span>" + $input.attr('placeholder') + "</span>"); Is there a way to make the placehol ...

Having difficulties showing live data in real-time, the Python web application is encountering an "Internal Server Error" while attempting to do so

The application is receiving real-time data (verified by using an alert), but when trying to display it on the card, an Internal Server Error occurs. The same code runs fine as an independent HTML page but not in a Flask Python web app. <!DOCTYPE ...

When navigating using the next and back buttons, the active state in Angular is automatically removed

Looking for some assistance with my quiz app setup. Each question has True/False statements with corresponding buttons to select T or F. However, when I click the next/back button, the active class is not being removed from the previous selection. As a beg ...

What is the process for toggling the expansion and collapse of a table row (tr)?

Based on my previous experience (such as this example), I have observed that a div container can be easily toggled to hide and show. However, I am facing confusion when I have content inside a tr and I wish to display and hide certain items when that tr is ...

Sending an array from one page to another with Vue

I'm facing the challenge of passing an array from one page to another. When accessing the next page on form submission using this.$router.push('path'), is there a way for me to also transfer the array so that it can be accessed on the new p ...

How to modify and remove a card element in semantic UI with react coding

I have recently created some cards and now I want to enhance the user experience by allowing them to edit the card data and delete the card altogether if they choose to do so. For the deletion functionality, here is an example of deleting a card using jQu ...