未处理拒绝SubmissionError:提交验证失败

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了未处理拒绝SubmissionError:提交验证失败相关的知识,希望对你有一定的参考价值。

我正在使用redux-form启用服务器端验证,当用户提交SignUp表单时,如果服务器响应错误,则表单显示错误...在这种情况下,错误的状态为422从服务器,并正在响应w {"errors":{"email":["has already been taken"]}}

当表单提交正常时,我无法在表单上显示SubmissionError。我在JS控制台中收到以下错误:

Unhandled rejection SubmissionError: Submit Validation Failed

我对redux-form提交验证处理有什么问题?谢谢

SignUpForm

const ensureValid = response => {
  if (response.status === 422) {
    return response.json().then(({errors}) => {
      throw new SubmissionError({
        email: 'Already in Use',
        _error: 'failed!',
      });

    });
  }
  return response;
};

class SignUp extends React.Component {

  handleSubmit(data) {
    const request = new Request('http://localhost:4400/auth/', {
     method: 'POST',
     headers: new Headers({
       'Accept'       : 'application/json',
       'Content-Type' : 'application/json',
     }),
     body: JSON.stringify({ user: data })
    });

    fetch(request).then(ensureValid).then(response => {
      return tryLogin(response, this.props.dispatch);
    });


   }

  render() {
    const { error, handleSubmit, pristine, reset, submitting } = this.props;
    return (
      <div className="container">
        <div className="row justify-content-md-center">
          <div className="col-6">

            <h1>Sign Up - it's free.</h1>

            <form onSubmit={this.props.handleSubmit(this.handleSubmit.bind(this))}>

              <Field
                name="first_name"
                type="text"
                component={renderField}
                label="First Name"
              />

              <Field
                name="last_name"
                type="text"
                component={renderField}
                label="Last Name"
              />

              <Field
                name="email"
                type="text"
                component={renderField}
                label="Email"
              />

              <Field
                name="password"
                type="password"
                component={renderField}
                label="Password"
              />

              <button type="submit" disabled={submitting}>Submit</button>

              {error && <strong>{error}</strong>}
            </form>

          </div>
        </div>
      </div>
    );
  }
}

// Decorate the form component
SignUp = reduxForm({
  form: 'SignUp' // a unique name for this form
})(SignUp);

const mapStateToProps = state => {
  return {
  };
};

const mapDispatchToProps = (dispatch) => bindActionCreators({
  dispatch
}, dispatch)

export default connect(mapStateToProps, mapDispatchToProps)(SignUp);
答案

结果我需要更新:

之前:

fetch(request)...

后:

return fetch(request) ...

以上是关于未处理拒绝SubmissionError:提交验证失败的主要内容,如果未能解决你的问题,请参考以下文章

(节点:32032)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 id:1):MongoError:身份验证失败

未处理的拒绝 SequelizeConnectionError:用户“ankitj”的密码验证失败

Ldap 用户授权失败 - 未处理的 Spring 身份验证“访问被拒绝”

REACT JS:未处理的拒绝(TypeError):无法读取未定义的属性“数据”

如何修复可能的未处理承诺拒绝(id:0)?以及如何修复无法读取未定义的属性“导航”?

设置firebase时出错,可能会出现未处理的承诺拒绝