question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

$bvToast not accessible from axios interceptor

See original GitHub issue

Describe the bug

When accessing $bvToast from inside a axios interceptor it comes up as undefined.

Steps to reproduce the bug

axios.js

import axios from 'axios';
import VueAxios from 'vue-axios';
import Vue from 'vue';
Vue.use(VueAxios, axios);

bootstrap.js

import Vue from 'vue';
import '@/plugins/bootstrap_custom.scss';
import Toast from 'bootstrap-vue/es/components/toast';
Vue.use(Toast);

axiosErrorHandler.js

import axios from 'axios';
import Vue from 'vue';
axios.interceptors.response.use(r => r, (error) => {
  console.log(Vue.prototype);
  Vue.prototype.$bvToast.toast(`Server call returned error ${error.response.status}`, {
    title: 'Error',
    variant: 'error',
  });
  return Promise.reject(error);
});

main.js

...
import '@/plugins/bootstrap';
import '@/plugins/axios';
import '@/plugins/axiosErrorHandler';
...

-> $bvToast shows up at the console.log as undefined -> get $bvToast shows up at the console.log at location bv-toast.js?3163:243 -> Error because Vue.prototype.$bvToast is undefined -> using this.$bvToast from an component works fine

Expected behavior

Vue.prototype.$bvToast is defined and Vue.prototype.$bvToast.toast works. OR Some remark about this behavior in the docs AND / OR A guide how to correctly implement this in the docs

Versions

Libraries:

  • BootstrapVue: 2.0.0-rc.19
  • Bootstrap: 4.3.1
  • Vue: 2.6.10
  • Axios: 0.18.0

Environment:

  • Device: PC
  • OS: Win 10
  • Browser: Chrome
  • Version: 74.0.3729.131

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

21reactions
xorikcommented, Jul 16, 2019

For anyone who faced the issue, here’s working code:

    axios.interceptors.response.use(
      response => response,
      error => {
        // You can extract message from your API, if you want
        const message = error.response.data.error.message || error.response.statusText
        const vm = new Vue()
        vm.$bvToast.toast(`Server call returned error: ${message}`, {
          title: `${error.response.statusText} (${error.response.status})`,
          variant: 'danger',
        })

        return Promise.reject(error)
      },
    )
3reactions
tmorehousecommented, May 9, 2019

To get it working (although it won’t have access to the global app $root instance for using show() method):

import axios from 'axios';
import Vue from 'vue';
axios.interceptors.response.use(r => r, (error) => {
  const vm = new Vue.extend({})
  vm.$bvToast.toast(`Server call returned error ${error.response.status}`, {
    title: 'Error',
    variant: 'error',
  });
  return Promise.reject(error);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

vue.js, axios, interceptors and toast-notifications
In my example I use interceptors to measure the time an api call takes, and to show a toast "fetching data" automatically, so...
Read more >
Axios request interceptor is not working in my vue app
I'm trying to make axios working with a request interceptor. However before a request is made the interceptor is not triggered. What could...
Read more >
[Solved]-Bootstrap-vue dynamic toast isn't working-Vue.js
In the module, the this keyword does not refer to the Vue component. The simplest workaround might be to make a second argument...
Read more >
How to run Bootstrapvue Toasts inside vuex Module - Laracasts
when I print (this.$bvToast) to console, it gives undefined.. Copy Code createJob ({commit}, payload) { axios.post( ...
Read more >
ルミックス 20mm F1.7を導入 - T2O diary
ReferenceError : Cannot access 'MockMyComponent' before initialization. ... When accessing $bvToast from inside a axios interceptor it comes up as undefined.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found