简体   繁体   English

从handleSubmit函数中抛出redux-form提交错误

[英]throwing redux-form submissions errors from handleSubmit functions

I have a redux-form which has a handleSubmit function which I use to make some async http server authentication. 我有一个redux-form,它有一个handleSubmit函数,我用它来做一些异步的http服务器验证。 If the authentication fails for some reason, I want to throw a redux-form error. 如果由于某种原因身份验证失败,我想抛出一个redux-form错误。 If I throw a "SubmissionError" I get an error message saying: Uncaught (in promise) 如果我抛出“SubmissionError”,我会收到一条错误消息: Uncaught (in promise)

My code: 我的代码:

 class MyLoginForm extends Component {    handleSubmit({ email, password }) {        axios.post("http://API_SERVER/login", null, { withCredentials: true, auth: { username: email, password: password } })            .then((response) => {                console.log('HTTP call success. JWT Token is:', response.data);            })            .catch(err => {                console.log(err) >>>> WHAT TO DO TO CONVEY THE ERROR IN THE REDUX-FORM <<<<                if (err.response) {                                        if (err.response.status !== 200) {                        console.log("Unexpected error code from the API server", err.response.status);                        throw new SubmissionError({ _error: 'HTTP Error. Possibly invalid username / password' });                    }                    return                }                throw new SubmissionError({ _error: 'HTTP Error. Please contact Helpdesk' });            });    }    render() {        const { error, handleSubmit, pristine, reset, submitting } = this.props        return (            <form onSubmit={handleSubmit(this.handleSubmit.bind(this))}>                <Field name="email" type="email" component={renderField} label="Email Address" />                <Field name="password" type="password" component={renderField} label="Password" />                {error && <strong>{error}</strong>}                <div>                    <button type="submit" disabled={submitting}>Log In</button>                    <button type="button" disabled={pristine || submitting} onClick={reset}>Clear Values</button>                </div>            </form>        )    } } export default connect(null, actions)(reduxForm({    form: 'MyLoginForm' // a unique identifier for this form })(MyLoginForm)); 

I want a solution to update/render the error message in the redux-form itself without creating anything in the global state, reducers, etc. 我想要一个解决方案来更新/呈现redux-form本身的错误消息,而不在全局状态,reducers等中创建任何东西。

The solution is simple. 解决方案很简单。 I just had to add a return in front of the axios call. 我只需要在axios电话前添加一个return So the updated code will be like: 所以更新的代码将是:

handleSubmit({ email, password }) {
    return axios.post("http://API_SERVER/login", null, { withCredentials: true, auth: { username: email, password: password } })
        .then((response) => {
            console.log('HTTP call success. JWT Token is:', response.data);
        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM