reactjs - 未处理的拒绝提交错误 : Submit Validation Failed

标签 reactjs react-redux bluebird redux-form react-redux-form

我正在努力使用 redux-form 启用服务器端验证,当用户提交注册表单时,如果服务器响应错误,表单会显示错误...在这种情况下,错误有一个来自服务器的 422 状态,并且正在响应 {"errors":{"email":["has has already been take"]}}

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

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

我在 redux-form 提交验证处理中做错了什么?谢谢

注册表单

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) ...

关于reactjs - 未处理的拒绝提交错误 : Submit Validation Failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44871376/

相关文章:

javascript - 我可以用 bluebird Promises 提前打破链条吗?

bluebird - 中断 Bluebird .each() 进程

javascript - 用 React.js 组件替换部分 html 字符串并渲染(没有 dangerouslySetInnerHTML)

javascript - 如何不允许用户在带有选择选项的输入字段中输入值

firebase - 带有 Firebase 的 GatsbyJS - WebpackError : ReferenceError: IDBIndex is not defined

javascript - 使用 superagent-bluebird-promise 在 Node 服务器上处理服务器重定向

javascript - Elasticsearch 索引数据在查询中给出错误结果

javascript - React Js - 如何保持 bundle.min.js 小?

javascript - React 中特定样式的 HTML 标签

javascript - React-redux 存储状态和 mapStateToProps 状态不同的原因是什么?