首页 文章

onSubmit函数中的Redux-Form v6.5.0 SubmissionError未传递字段错误/ _error,在控制台中显示“Uncaught(in promise)”错误

提问于
浏览
1

因此,与Redux-Form一起,我使用axios和thunk作为中间件,当我在onSubmit函数(loginUser)时,并使用Axios进行AJAX调用 . 不幸的是,当我想发信号通知我的用户提交的凭据无效并抛出一个SubmissionError来表示onSubmit函数失败,因此得到表单上显示的错误,我得到“Uncaught(in promise)” .

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

目前使用的版本是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

    对于那些你想知道的人,我使用'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);
    
        });
       })
    }
    

相关问题