【问题标题】:VUE and Axios API : Passing error code from api, to store, to vue componentVUE 和 Axios API:将错误代码从 api 传递到 store 到 vue 组件
【发布时间】:2018-03-13 23:18:56
【问题描述】:

这很好,可能会被标记为重复,如果是我很抱歉。一直在谷歌上搜索很多以找到如何实际执行此操作,但没有任何适当的解决方案(虽然我不是 vue 专家..)

基本上..我正在尝试做什么..从 api => store => vue 组件传递成功或失败。如果错误...我将向用户显示错误代码(现在...)

事情的方式..

1) 从 vue 组件触发的方法。发送到 $store (modal.vue)

2) 触发状态动作来设置突变类型和调用API。

3) Api 方法被调用。

4) 返回成功或错误,以及 http.statuscode....

MODAL.VUE

doRefund: function(){
            this.$store.dispatch('doRefund', {
                    Username : this.loggedInUser.account.username,
                    OrderID: this.selectedOrder.orderid,
                    IsFeeApplied: false,
                    CreditAmount: this.refundAmount,
                    ChargeFee: 0.0,
                    Reason: "reason-not-specified",
                    Description: this.comment,
                    BearerToken: "Bearer " + this.accessToken
            })
            .then(result => {
                if(result === true){
                  alertify.success('It worked!')
                }
                else{
                    alertify.alert('There was an error, and the errorcode is' + errorcode ????)
                }
            })
        }

STORE.JS

doRefund({ commit }, refundParams){
        api.tryRefund(refundParams)
        .then(refundCompleted => {
            commit(types.SET_REFUND_COMPLETED, true)
            return true;
        })
        .catch(err => {
            //TODO: How to i fetch, and pass the errorcode ? 
            commit(types.SET_REFUND_COMPLETED, false)
            return false;

        })
    },

API.JS

tryRefund(refundParams) {
    console.log('=== try ====');
    console.log( refundParams );
    return new Promise((resolve, reject) => {
        var config = {
            headers: {
                'Content-Type': ' application/json',
                'Authorization': refundParams.BearerToken
            }
        };
        return axios.post('the-url-to-the-service', refundParams, config)
            .then(
                () => resolve(true))
            .catch( error => {
                console.log('=== ERROR ====');
                console.log( error.response );

            })
    });
}

【问题讨论】:

    标签: vue.js vuejs2 axios


    【解决方案1】:

    您需要在api.js 文件中的tryRefund 方法中将error.response 传递给reject 处理程序:

    .catch(error => {
      console.log('=== ERROR ====');
      console.log( error.response );
      reject(error)
    })
    

    那么,你应该在doRefund动作方法中抛出错误:

    .catch(err => {
      //TODO: How to i fetch, and pass the errorcode ? 
      commit(types.SET_REFUND_COMPLETED, false)
      throw err;
    })
    

    然后在 $dispatch 方法的 catch 处理程序中捕获它:

    this.$store.dispatch('doRefund', {
      ...               
    })
    .then(result => {
      ...
    })
    .catch(error => { 
      console.log(error); // this is the error you want
    })
    

    【讨论】:

    • 谢谢@thanksd :) 这正是我所追求的正是 :)
    • 关于如何做的简单而可怕的描述......希望我提出的每个问题都得到这样的回答。
    • 只有奇怪的事情.. 似乎 $dispatch 被触发了两次?知道为什么吗?
    • 这意味着由于某种原因,模态组件的doRefund 方法被多次调用。您的帖子中没有足够的上下文并且绝对不相关,因此如果您无法弄清楚,我会发布一个带有相关代码的新问题
    • @thanksd 如果他消除了商店中的.catch(例如,他不需要提交SET_REFUND_COMPLETED 然后reject 自动将其扔到此模式中的最终捕获?只是好奇因为它似乎解决这个问题的唯一方法是有一个拒绝 - 抛出 - 拒绝模式
    猜你喜欢
    • 2017-02-27
    • 2017-02-24
    • 2019-08-03
    • 2019-02-05
    • 2019-03-28
    • 2022-11-14
    • 2021-03-01
    • 1970-01-01
    • 2018-08-24
    相关资源
    最近更新 更多