reactjs - 使用 React 最终形式和 Axios 显示 Api 错误
问题描述
我正在尝试使用 React Final Form 和 Axios 来显示从我的 Api 返回的错误以进行 API 调用。当我从 Axios 返回 .catch() 中的错误时,似乎表单没有重新加载错误消息。
正如您在注释掉的返回顶部看到的那样,如果我在那里调用它,它会在表单中正确显示错误。虽然当我在 catch() 中调用它时,它没有显示任何错误。
我想要做的是在捕获中返回错误,例如当 API 发送 400 响应时。
我的 onSubmit 如下,而 axiosConfig 只是一个拦截器,用于将令牌附加到 api 调用。我也用普通的 axios 对其进行了测试,但行为是一样的。
const onSubmit = async values => {
//return { [FORM_ERROR]: "Login Failed" };
axiosConfig.put('/api/v1/user/change-password/' + userPk, values, {
'headers': { 'Authorization': auth }
}).then( result => {
if (result.status === 200) {
setChanged(true);
}
}).catch((error) => {
console.log(error)
return { [FORM_ERROR]: "Login Failed" };
});
};
我的表格看起来像这样。
return (
<div style={{ padding: 16, margin: 'auto', maxWidth: 650 }}>
<Form
onSubmit={onSubmit}
initialValues={{ }}
validate={validate}
render={({
submitError,
handleSubmit,
reset,
submitting,
pristine,
values
}) => (
<form onSubmit={handleSubmit} noValidate>
<Paper style={{ padding: 16 }}>
<Typography variant="h5" align="center" component="h2" gutterBottom>
Passwort Ändern
</Typography>
{console.log(submitError)}
<div>
<Grid container alignItems="center" justify="center" spacing={4}>
<Grid item>
<Field
fullWidth
required
margin="normal"
variant="outlined"
component={TextField}
name="old_password"
label="Altes Passwort"
type="password"
/>
<Field
fullWidth
required
margin="normal"
variant="outlined"
type="password"
name="new_password"
component={TextField}
label="Neues Password"
/>
<Field
fullWidth
required
margin="normal"
variant="outlined"
type="password"
name="new_password2"
component={TextField}
label="Neues Password Wiederholen"
/>
</Grid>
</Grid>
</div>
<div>
{submitError && <div className="error">{submitError}</div>}
<Grid container alignItems="flex-start" justify="space-between" spacing={4}>
<Grid item xs container spacing={4}>
<Grid item style={{ marginTop: 16 }}>
<Button
component={ Link }
to="/account/information"
type="button"
variant="contained"
>
Zurück
</Button>
</Grid>
<Grid item style={{ marginTop: 16 }}>
<Button
variant="contained"
color="primary"
type="submit"
>
Speichern
</Button>
</Grid>
</Grid>
</Grid>
</div>
</Paper>
</form>
)}
/>
</div>
);
解决方案
尝试使用async
/await
语法。
也可能存在这样的情况,即您的错误块首先没有被调用,并且控件仍在成功回调中。因此,如果状态码不是 200,您可能还想在其中放置一个 else 条件以返回错误。
像这样的东西:
const onSubmit = async values => {
try {
const result = await axiosConfig.put('/api/v1/user/change-password/' + userPk, values, {
'headers': {
'Authorization': auth
}
});
if (result.status === 200) {
setChanged(true);
} else {
// You Might also want to try this!
return {
[FORM_ERROR]: "Login Failed"
};
}
} catch (error) {
console.log(error)
return {
[FORM_ERROR]: "Login Failed"
};
}
};
推荐阅读
- python - 使用 ConfigParser 在 python 中读取配置文件
- sharepoint - 如何修复 SharePoint 设计器工作流发布问题?
- vue.js - 如何在带有 div 的 vue js 中动态显示值
- php - 如何在php中比较开始和结束时间与当前时间?
- python - 为什么在训练期间多次缩小参数将有助于 Progressive GAN 中所有权重的学习速度相同?
- c# - 如何在winform中的禁用按钮上显示工具提示?
- python - 无法从 pubmed xml 文件中读取所有抽象文本
- architecture - 清洁架构 - “组件原则三合会”的示例
- php - 语法错误,在 laravel 8 中出现意外的 ')'。\vendor\laravel\framework\src\Illuminate\Bus\BusServiceProvider.php:51
- android - 如何查看 RecycleView 中的数据?