Javascript 使用Axios在两个位置轻按错误消息
我们所有的api调用都在一个api.js文件中,如下所示:Javascript 使用Axios在两个位置轻按错误消息,javascript,promise,axios,Javascript,Promise,Axios,我们所有的api调用都在一个api.js文件中,如下所示: import { axiosClient as axios } from 'Utils'; export const urls = { getReceiptData: '/purchase/getReceiptData', processPurchase: '/purchase/process-purchase', }; export const getReceiptData = () => axios.get(ur
import { axiosClient as axios } from 'Utils';
export const urls = {
getReceiptData: '/purchase/getReceiptData',
processPurchase: '/purchase/process-purchase',
};
export const getReceiptData = () => axios.get(urls.getReceiptData)
.then(({ data }) => data);
export const purchaseStatus = purchaseId => axios.get(urls.purchaseStatus.replace('$1', purchaseId))
.then(({ data }) => data);
export const purchaseStatus = purchaseId => axios.get(urls.purchaseStatus.replace('$1', purchaseId))
.then(({ data }) => data, error => newrelic.noticeError(error););
但是,我们希望开始将所有错误消息记录到New Relic,当然不会破坏功能
我最初的反应是接收此文件中的错误消息并在此处处理,如下所示:
import { axiosClient as axios } from 'Utils';
export const urls = {
getReceiptData: '/purchase/getReceiptData',
processPurchase: '/purchase/process-purchase',
};
export const getReceiptData = () => axios.get(urls.getReceiptData)
.then(({ data }) => data);
export const purchaseStatus = purchaseId => axios.get(urls.purchaseStatus.replace('$1', purchaseId))
.then(({ data }) => data);
export const purchaseStatus = purchaseId => axios.get(urls.purchaseStatus.replace('$1', purchaseId))
.then(({ data }) => data, error => newrelic.noticeError(error););
但在从循环调用api.js文件时,我无法获得相同的功能:
pollPurchase = purchaseId => new Promise(resolve => setTimeout(resolve, 1000))
.then(() => api.purchaseStatus(purchaseId))
.then((response) => {
if (response.state === 'COMPLETED') {
return true;
}
return this.pollPurchase(purchaseId);
});
这似乎打破了循环。
有人对此有什么建议吗?Axios使用承诺模式而不是回调模式,因此请尝试使用此模式
export const purchaseStatus = purchaseId =>
axios.get(urls.purchaseStatus.replace('$1', purchaseId))
.then(({ data }) => data)
.catch(error => {
newrelic.noticeError(error);
return Promise.reject(error);
});
它的工作原理应该与您的解决方案相同,您试图传递一个.2次回调,但它只接受第一次回调,而忽略第二次回调
并将pollPurchase方法更新为
pollPurchase = purchaseId => new Promise(resolve => setTimeout(resolve, 1000))
.then(() => api.purchaseStatus(purchaseId))
.then((response) => {
if (response.state === 'COMPLETED') {
return true;
}
return this.pollPurchase(purchaseId);
}).catch(console.error)
非常感谢。这确实有帮助,现在它没有出现错误。但我们应用程序其余部分的功能现在无法继续。在调用api.js的文件中还有什么需要做的吗?PollPurchaseId=>new Promise(resolve=>setTimeout(resolve,1000)).then(()=>api.purchaseStatus(purchaseId)).then((response)=>{if(response.state=='COMPLETED'){return true;}返回这个.pollPurchaseId;})@TuxedoJoe它特别破坏了什么功能?如果您以前的代码正常工作,我建议的更改预计不会中断,除非您没有处理错误,并且总是希望得到数据响应。问得好!我已经更新了没有像以前一样继续的代码。@TuxedoJoe更新了答案,早些时候的问题是我没有拒绝catch块,这意味着中的响应。然后pollPurchase的未定义,因此在您尝试执行
response.state
的未定义时导致引用错误,因此您的应用程序失败,现在我抛出了错误,这意味着错误案例将不会进入。然后,但它将被接收。捕获pollPurchaseYou是我的人做的!谢谢@MohitYadav为newrelic的类型添加了一个检查!='在我运行newrelic脚本之前未定义,因为当我得到ReferenceError时,这给了我奇怪的问题。再次感谢!