Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I return error message from axios function inside Vuex action to dispatch catch(error)?

Tags:

vue.js

vuejs2

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

like image 563
Utsav Shrestha Avatar asked Aug 07 '19 10:08

Utsav Shrestha


People also ask

How do I dispatch an action from action in Vuex?

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) { // }, }, });

Does Vuex Dispatch return a promise?

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.

What is $store in Vue?

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.

What is the use of dispatch in Vuex?

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.


2 Answers

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

like image 151
Abdelillah Aissani Avatar answered Sep 20 '22 16:09

Abdelillah Aissani


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;
        }
    },
}
like image 42
kapa89 Avatar answered Sep 23 '22 16:09

kapa89