Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Reactjs+;Redux未处理拒绝(TypeError):无法读取属性';数据';未定义的_Javascript_Reactjs_Redux_Async Await_React Redux - Fatal编程技术网

Javascript Reactjs+;Redux未处理拒绝(TypeError):无法读取属性';数据';未定义的

Javascript Reactjs+;Redux未处理拒绝(TypeError):无法读取属性';数据';未定义的,javascript,reactjs,redux,async-await,react-redux,Javascript,Reactjs,Redux,Async Await,React Redux,在使用react+redux实现身份验证时,当我尝试使用async/await在redux的操作中注册用户时,我在catch中得到此错误,以下是操作代码: import axios from 'axios'; import { setAlert } from './alert'; import { REGISTRO_CORRECTO, REGISTRO_FALLIDO } from './types'; // Registro de Usuario export const registro

在使用react+redux实现身份验证时,当我尝试使用async/await在redux的操作中注册用户时,我在catch中得到此错误,以下是操作代码:

import axios from 'axios';
import { setAlert } from './alert';
import { REGISTRO_CORRECTO, REGISTRO_FALLIDO } from './types';

// Registro de Usuario
export const registro = ({
  usuario,
  interfac,
  password,
  descripcion
}) => async dispatch => {
  const config = {
    headers: {
      'Content-Type': 'application/json'
    }
  };

  const body = JSON.stringify({ usuario, password, interfac, descripcion });

  try {
    // Conex to backend
    const res = await axios.post('http://localhost:5000/api/usuarios', body, config);

    dispatch({
      type: REGISTRO_CORRECTO,
      payload: res.data
    });
  } catch (err) {
    const errors = err.response.data.errors;

    if (errors) {
      errors.forEach(error => dispatch(setAlert(error.msg, 'danger')));
    }

    dispatch({
      type: REGISTRO_FALLIDO
    });
  }
};
运行submit时会出现以下错误:

  26 |      payload: res.data
  27 |    });
  28 |  } catch (err) {
> 29 |    const errors = err.response.data.errors;
     | ^  30 | 
  31 |    if (errors) {
  32 |      errors.forEach(error => dispatch(setAlert(error.msg, 'danger')));

致以最诚挚的问候

您收到了错误,因为对象数据中不存在字段
errors
。在这种情况下,在引用返回对象以从中获取数据之前,它应该设置一些条件来检查返回对象的格式

因此,您可以参考下面的程序检查

  } catch (err) {
    if(err.response.data && err.response.data.message){
       // todo
    }else if(err.response.data && err.response.data.errors){
       // todo
    }

err.response.data.errors您有一个错误,假设您的数据将被填写?。。只需尝试控制台日志记录错误。你很可能会发现真正的错误。很遗憾,您在报告错误时遇到错误:)Keith是真的,我在catch下面放了一个console.log(err),它向我返回这个错误:网络错误,在我看来axios不适合我。考虑到最后的问题是后端的问题,我没有宣布CORS,因为前端代理不工作,axios出现了网络问题,这就是为什么我没有得到答案。雷加索克!您应该为API的头响应添加CORS属性。然后,前端可以与具有不同来源访问权限的后端集成。