Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property '$axios' of undefined nuxtjs vuex

Tags:

nuxt.js

vuex

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.

like image 678
manshu Avatar asked Sep 18 '25 06:09

manshu


2 Answers

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);
  });
};
like image 123
TenshiGoan Avatar answered Sep 21 '25 02:09

TenshiGoan


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.

like image 22
jedmao Avatar answered Sep 21 '25 03:09

jedmao