gpt4 book ai didi

vue.js - VUE和axios API : Passing error code from api,来存储,到vue组件

转载 作者:搜寻专家 更新时间:2023-10-30 22:32:17 24 4
gpt4 key购买 nike

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

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

事物的方式..

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

2) State action被触发设置mutationtype和调用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 );

})
});
}

最佳答案

您需要通过 error.responsereject你的处理程序 tryRefund api.js 中的方法文件:

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

然后,你应该在 doRefund 中抛出错误 Action 方法:

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

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

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

关于vue.js - VUE和axios API : Passing error code from api,来存储,到vue组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46533084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com