简体   繁体   English

我正在尝试调度一个操作,但收到此错误:“未处理的拒绝(TypeError):无法读取未定义的属性'类型'”

[英]I am trying to dispatch a action but getting this error: “ Unhandled Rejection (TypeError): Cannot read property 'type' of undefined”

import axios from "axios";

export function login(content) {
  console.log(content);
  return dispatch => {
    axios.post("/v1/login", content).then(res => {
      console.log(res);
      if (res.data.response.userResponse.userResponseCode == 20000) {
        dispatch(loginSuccess(true));
      }
    });
  };
}

function loginSuccess(isLogginSuccess) {
  console.log(isLogginSuccess);
  return {
    type: LOGIN_SUCCESS,
    isLogginSuccess
  };
}

export default function reducer(
  state = {
    isLogginSuccess: false
  },
  action
) {
  switch (action.type) {
    case LOGIN_SUCCESS:
      return {
        ...state,
        isLogginSuccess: action.isLogginSuccess
      };
    default:
      return state;
  }
}

While doing user login I am calling this action.在进行用户登录时,我正在调用此操作。 If user successfully logged in, isLogginSuccess will be true.如果用户成功登录, isLogginSuccess将为真。 Trying to call action, but getting error.. please anyone help..试图采取行动,但得到错误..请任何人帮助..

Axios returns a promise, which can resolve or reject. Axios 返回一个 promise,可以解析或拒绝。 In your case it is returning a rejection and your code isn't handling it.在您的情况下,它返回拒绝并且您的代码没有处理它。 Typically you'd catch it and dispatch some failure action, log it, or ignore it, anything else, but you still need to catch the rejected promise.通常,您会捕获它并调度一些失败操作、记录它或忽略它,但您仍然需要捕获被拒绝的 promise。

For example:例如:

export function login(content) {
  console.log(content);
  return dispatch => {
    axios
      .post("/v1/login", content)
      .then(res => {
        console.log(res);
        if (res.data.response.userResponse.userResponseCode === 20000) {
          dispatch(loginSuccess(true));
        }
      })
      .catch(() => {
        dispatch(loginSuccess(false));
      });
  };
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “未处理的拒绝(TypeError):无法读取未定义的属性'dispatch'” react-redux 错误 - “Unhandled Rejection (TypeError): Cannot read property 'dispatch' of undefined” react-redux error 未处理的拒绝(TypeError):无法读取未定义的属性 - Unhandled Rejection (TypeError): Cannot read property of undefined axios:未处理的拒绝(TypeError):无法读取未定义的属性“错误” - axios: Unhandled Rejection (TypeError): Cannot read property 'error' of undefined React - 未处理的拒绝(TypeError):无法读取未定义的属性“错误” - React - Unhandled Rejection (TypeError): Cannot read property 'error' of undefined React API错误(未处理的拒绝(TypeError):无法读取未定义的属性“ 0”) - React API Error (Unhandled Rejection (TypeError): Cannot read property '0' of undefined) 来自 api 的用户搜索得到错误的过滤响应:`未处理的拒绝(TypeError):无法读取未定义的属性'结果' - Filter response from api on user search getting error :`Unhandled Rejection (TypeError): Cannot read property 'results' of undefined` 我收到错误:-未处理的拒绝(TypeError):无法读取 React 应用程序中未定义的属性“id” - I get the error : - Unhandled Rejection (TypeError): Cannot read property 'id' of undefined in React Application 错误:未处理的拒绝(类型错误):无法读取未定义的属性“地图”不确定我错过了什么 - Error: Unhandled Rejection (TypeError): Cannot read property 'map' of undefined not sure what I'm missing 未处理的拒绝(TypeError):分派不是在动作创建者中获取此错误的函数 - Unhandled Rejection (TypeError): dispatch is not a function getting this error in action creator 请问我该如何处理这个错误“未处理的拒绝(类型错误):无法读取未定义的属性'过滤器'” - Please how do i handle this error “Unhandled Rejection (Type Error): Cannot read property 'filter' of undefined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM