I have an action where ajax call is made using axios,whenever the axios returns errors it gets catch by axios catch function , so i want to know if its possible to throw that same error to dispatch catch function.
I've tried to //throw new Error("test error inside"); from inside of axios catch(error) but dispatch doesnot seem to catch the error
Action code on vuex store
actions:{
assignOrder(context, assign){
axios.post('/assignOrder',assign)
.then((response) => {
console.log(response)
})
.catch((error) => {
//I want to throw error catched from here
console.log(error.response.data.errors)
//throw new Error("test error inside");
// }
})
}
}
On my method on vue component
methods:{
setAssign(){
this.assign.order_id = this.order.id
if(this.validate()){
this.errors = {};
this.$store.dispatch('assignOrder', this.assign).then(() => {
showNotify('success','Order has been assigned')
this.$store.dispatch('getOrders',this.active)
})
.catch((error) => {
//catch the error here
alert(error)
})
}
else{
this.showErr = true;
}
},
}
I want the axios to throw catch error which will catch by dispatch
You can call another Vuex action by passing the name of that action as a string as the first argument of dispatch : const store = new Vuex. Store({ actions: { walk(context) { context. dispatch("goForward"); }, goForward(context) { // }, }, });
dispatch('action') function returns a promise. This allows the programmer to wait for an action to be completed before executing another one. Let's see an example where an action is dispatched after another one is completed: store.
In Vue, as well as other front end frameworks like React, a store is a centralized location to keep up with data that is available across all the application components. A logged in user is a perfect example of data that belongs in a store.
Dispatch triggers an action whereas commit triggers a mutation. $dispatch is always used from your methods in routes/components. It sends a message to Vuex store to perform some action. The action can be performed any time after the current tick so that it will not affect the frontend performance.
Just return a promise from your action then handle that on your component :
actions: {
assignOrder(context, assign) {
return new Promise((resolve, reject) => {
axios.post('/assignOrder', assign)
.then((response) => {
resolve(response)
})
.catch((error) => {
reject(error.response.data.errors)
})
})
}
}
and on your component :
methods: {
setAssign() {
this.assign.order_id = this.order.id
if (this.validate()) {
this.errors = {};
this.$store.dispatch('assignOrder', this.assign).then((res) => {
showNotify('success', 'Order has been assigned')
console.log(res)
this.$store.dispatch('getOrders', this.active)
})
.catch((error) => {
// catch the error
alert(error)
})
} else {
this.showErr = true;
}
},
}
The promise will return either a resolve
or a reject
which will be bound to your then
or catch
You can just return Promise in action and handle the response/error in method
vuex store:
actions:{
assignOrder(context, assign){
return axios.post('/assignOrder',assign)
}
}
and in your component do the following:
methods:{
setAssign() {
this.assign.order_id = this.order.id
if (this.validate()) {
this.errors = {};
this.$store.dispatch('assignOrder', this.assign).then(() => {
showNotify('success','Order has been assigned')
this.$store.dispatch('getOrders', this.active)
}).catch((error) => {
//catch the error here
alert(error)
})
} else{
this.showErr = true;
}
},
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With