Reputation: 91
I am new in vue and frontend, I need to get categories, then parse it to subcategories, what i am doing wrong? There is error 'is not a function'. Ho to do it in the correct way?
GET_CATEGORIES_FROM_API({commit}) {
return axios('http://localhost:3000/categories',
{
method: "GET"
}).then((categories) => {
commit('SET_CATEGORIES_TO_STATE', categories.data)
return categories;
}).catch((error) => {
console.log(error);
return error;
})
},
GET_SUBCATEGORIES_FROM_CATEGORIES({commit}) {
dispatch(this.GET_CATEGORIES_FROM_API()).then(categories => { // here is an error
let subcategories = []
for (let category in categories) {
for (let subcategory in category) {
subcategories.push(subcategory.name)
}
}
commit('SET_SUBCATEGORIES_TO_STATE', subcategories)
return subcategories
})
}
Upvotes: 0
Views: 108
Reputation: 2371
Your syntax for dispatch is incorrect.
When dispatching, you use the same syntax as you do with commits. Simply pass a string reference to the name of the action like this
dispatch('MY_AWESOME_ACTION', ...optionalParams);
Upvotes: 1