[英]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.