0

Yes, there are a lot of post like this, but i can't figure out whats happening (look for a lot of post here and in Google).

I made my interceptor like this, but I'm getting a message "This is undefined".

axios.interceptors.response.use(undefined, function(err) {

    return new Promise(function(resolve, reject) {

        if (err.request.status === 401 && err.config && !err.config.__isRetryRequest) {

            console.log('Disconnected. Session expires')

            this.$store.dispatch('logout')
            this.$router.push({ name: "login" });

        }

        throw err;

    });

First I had "() => {}" and I have changed to "function() {}", but same error.

How can I do to clear my code?

Guido Caffa
  • 1,201
  • 1
  • 12
  • 22
  • try setting var _this = this, and then accessing it as _this. To verify if it is really this that is causing the problem. ()=>{} should have worked fine – user120242 Jun 18 '20 at 03:38
  • Just import your `store` and `router` from their respective files, eg `import store form 'path/to/store` and use `store.dispatch()`. Same thing with `router` – Phil Jun 18 '20 at 03:51

1 Answers1

2

this only work in vue components, I suspect your code in question is not in any component. Then you need to look for how to access store and router outside components: access store outside of component vuejs

JJPandari
  • 3,454
  • 1
  • 17
  • 24