onSubmit函数中的Redux-Form v6.5.0 SubmissionError未传递字段错误/ _error,给出" Uncaught(in promise)"控制台

时间:2017-02-19 10:59:24

标签: reactjs redux redux-form

所以,与Redux-Form一起,我使用axios和thunk作为中间件,当我在onSubmit函数(loginUser)时,并使用Axios进行AJAX调用。不幸的是,当我想要发出我的用户提交的凭据无效并发出SubmissionError来表示onSubmit函数失败时,并因此得到我正在获取的表单上显示的错误" Uncaught(在诺言中) )"

我从其他帖子中读到,我可能必须在某个时候返回一个承诺,但我并不完全确定如何实现(如果这甚至是问题)。

目前使用Redux-Form 6.5.0版本。任何帮助将不胜感激。

import axios from 'axios';  
import { SubmissionError } from 'redux-form';

export function loginUser({ email, password }) {  
  return function(dispatch) {
      axios.post(`${API_URL}/authenticate`, { email, password })
      .then(response => {
        console.log('status is: ', status, ' response is: ', response);
        if(response.data.token){
          cookie.save('token', response.data.token, { path: '/' });
          dispatch({ type: AUTH_USER });
          browserHistory.push('/');
        } else {
          if(response.data.success === false) {
            var errObj = { email: "Invalid email and password combo",     _error: "That email and password combination did not work. Please try     again."};
            throw (errObj)
          }
        }
      })
      .catch((error) => {
        throw(new SubmissionError(error));
      })
    }
  }

控制台出错:

Uncaught (in promise) >
SubmissionError
errors
:
Object
message
:
"Submit Validation Failed"
name
:
"SubmissionError"
stack
:
"SubmissionError: Submit Validation Failed↵    at eval (eval at     <anonymous> (http://localhost:8080/bundle.js:14:22671),     <anonymous>:94:1297)"
__proto__
:
ExtendableError

1 个答案:

答案 0 :(得分:0)

对于那些你想知道的人,我使用'resolve'和'reject'与reject()函数内部的SubmissionError(也注意顶部的新Promise部分):

export function registerUser({ email, password }) {  
  return new Promise((resolve, reject) => {
     axios.post('http://localhost:8088/api/users', { email: email, password: password })
    .then(response => {
       console.log('response is: ' , response, 'response.data is: ', response.data, 'response.code is: ', response.code);
      if(response.data.success){
        console.log('registerUser response.data.success is true')
        cookie.save('token', response.data.token, { path: '/' });
        store.dispatch({ type: AUTH_USER });
        browserHistory.push('/');
        resolve();
       } else {
        if(response.data.code === 11000){ //duplicate email
          console.log('data code = 11000')
          var errObj = new SubmissionError({_error: 'User registration failed, email already exists.' }) //need to add store dispatch for failed user registration (for form feedback)
          reject(errObj);
         } else if (response.code === 2) {
          console.log('response.code = 2')
          var errObj = new SubmissionError({ email: 'Invalid email pattern.' })
          reject(errObj);
         }
      }
    }).catch((error) => {
      console.log('error is: ', error)
      //errorHandler(store.dispatch, error, AUTH_ERROR)      
      if(error instanceof SubmissionError) reject(error);

    });
   })
}