been receiveing error like this in my Vuex Store
TypeError: Cannot read property 'get' of undefined
I already have added a plugin for the vue-resource
This is my code that is receiving that error
async getAllStudents({mit}) {
await Vue.$http.get(`/api/students`)
.then((res) => {
if (res.status === 200) {
mit('setAllStudents', res.data)
}
}).catch(function(error) {
console.log(error);
});
},
been receiveing error like this in my Vuex Store
TypeError: Cannot read property 'get' of undefined
I already have added a plugin for the vue-resource
This is my code that is receiving that error
async getAllStudents({mit}) {
await Vue.$http.get(`/api/students`)
.then((res) => {
if (res.status === 200) {
mit('setAllStudents', res.data)
}
}).catch(function(error) {
console.log(error);
});
},
Share Improve this question edited Jun 20, 2020 at 15:00 Boussadjra Brahim 1 asked Jun 20, 2020 at 14:29 Reinard CarrancejaReinard Carranceja 772 silver badges11 bronze badges 2- In which file are you doing this? – BeHappy Commented Jun 20, 2020 at 14:37
- In my store file - /store/enrollment – Reinard Carranceja Commented Jun 20, 2020 at 14:38
2 Answers
Reset to default 4Try to use this
instead of Vue
and try axios:
async getAllStudents({mit}) {
await this.$axios.get(`/api/students`)
.then((res) => {
if (res.status === 200) {
mit('setAllStudents', res.data)
}
}).catch(function(error) {
console.log(error);
});
},
make sure that your nuxt.config.js
looks like :
const routerBase = process.env.DEPLOY_ENV === 'GH_PAGES' ? {
router: {
base: '/boussadjra-brahim/'
}
} : {}
export default {
mode: 'universal',
/*
** Headers of the page
*/
head: {
titleTemplate: '%s - ' + 'Boussadjra Brahim',
title: 'Boussadjra Brahim' || '',
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: process.env.npm_package_description || '' }
],
link: [
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
],
},
/*
** Plugins to load before mounting the App
*/
plugins: [
],
....
/*
** Nuxt.js modules
*/
modules: [
// Doc: https://axios.nuxtjs/usage
'@nuxtjs/axios',
],
....
}
Try this, use this._vm
instead of Vue
:
async getAllStudents({mit}) {
await this._vm.$http.get(`/api/students`)
.then((res) => {
if (res.status === 200) {
mit('setAllStudents', res.data)
}
}).catch(function(error) {
console.log(error);
});
},