4

I stumbled upon this bug in my codebase and trying to see if anyone can fix it.

following is my listings/actions.js

export const fetchFeaturedListings = ({ commit }) => {
  this.$axios.get("/featured").then(response => {
    console.log(response.data.data);
    commit("listings/setFeaturedListings", response.data.data);
  });
};

I am constantly getting the following error.

Cannot read property '$axios' of undefined

I've searched everywhere, and still not able to find an answer. Hope someone can help.

1
  • Need more details like what is your environment? Webpack? Node? Commented Sep 17, 2019 at 3:58

3 Answers 3

5

For arrow function vuex can't set 'this'. Try to use standart functions.

export const fetchFeaturedListings = function({ commit }){
  this.$axios.get("/featured").then(response => {
    console.log(response.data.data);
    commit("listings/setFeaturedListings", response.data.data);
  });
};
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, to be clear, every time you use an arrow function you override the this context.
1

You're using an arrow function, which means this comes from the outer scope. If $axios doesn't exist in that outer scope, this is why you see this error.

Comments

0

Just like jedmao said, you are accessing a wrong this. suggest you just import axios and use it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.