2 回答

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊
如果您不想在捕獲錯(cuò)誤后執(zhí)行代碼,那么您應(yīng)該這樣做:
app.get('/submit/:imei', async function (req, res) {
//configure
res.setHeader('Content-Type', 'application/json');
MyUserAgent = UserAgent.getRandom();
axios.defaults.withCredentials = true;
try {
const model_info = await getModelInfo(req.params.imei);
console.log('This will not get called if there is an error in getModelInfo');
res.send({ success: true });
} catch(error) {
if(error.response && error.response.status === 406) {
return res.send({
'success': false,
'reason': 'exceeded_daily_attempts'
});
}
}
});
或者,您可以在通話then后使用,只有在不拒絕getModelInfo時(shí)才會(huì)調(diào)用該代碼。getModelInfo

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊
它也應(yīng)該有一個(gè)成功塊。
app.get('/submit/:imei', async function (req, res) {
//configure
res.setHeader('Content-Type', 'application/json');
MyUserAgent = UserAgent.getRandom();
axios.defaults.withCredentials = true;
const model_info = await getModelInfo(req.params.imei)
.then(function (response) {
return res.send(JSON.stringify({
'success': true,
'res': response
}));
})
.catch(function (error) {
if (error.response && error.response.status === 406) {
return res.send(JSON.stringify({
'success': false,
'reason': 'exceeded_daily_attempts'
}));
}
});
//it will console log
console.log('This still gets called even after 406 error!');
});
添加回答
舉報(bào)