[英]Im getting an error with my catch that i cant seem to figure out
I am fairly new to react but I think I'm starting to get the hang of it, and just as I thought that I ran into an issue that I cant seem to figure out.我对反应还很陌生,但我想我已经开始掌握它了,就像我认为我遇到了一个我似乎无法弄清楚的问题一样。 when logging in to the app I get this.登录应用程序时,我得到了这个。
Unhandled Rejection (TypeError): Cannot read property 'data' of undefined
(anonymous function)
src/actions/auth.js:105
102 | dispatch(push("/app"));
103 | })
104 | .catch((err) => {
> 105 | dispatch(authError(err.response.data));
| ^ 106 | });
107 | } else {
108 | dispatch(authError("Something was wrong. Try again"));
the code in question is below.有问题的代码如下。
export function authError(payload) {
return {
type: AUTH_FAILURE,
payload,
};
}
export function loginUser(creds) {
return (dispatch) => {
if (creds.email.length > 0 && creds.password.length > 0) {
axios
.post("/auth/login", creds)
.then((res) => {
const payload = res.data;
dispatch(receiveToken(payload));
dispatch(doInit());
dispatch(push("/app"));
})
.catch((err) => {
dispatch(authError(err.response.data));
});
} else {
dispatch(authError("Something was wrong. Try again"));
}
};
}
the thing is the server responds with "POST /api/auth/login HTTP/1.1" 200 286问题是服务器响应“POST /api/auth/login HTTP/1.1”200 286
so at this point, it looks like the server is getting the right creds, I don't know what to do.所以在这一点上,看起来服务器正在获得正确的信誉,我不知道该怎么做。 I'm putting in the right credentials that worked before and regardless if an error did occur it's not catching it correctly.我正在输入以前有效的正确凭据,无论是否确实发生了错误,它都没有正确捕获它。 Any help would be greatly appreciated.任何帮助将不胜感激。 Thank you谢谢
console.log(err) instead of dispatch(authError(err.response.data)) first in your catch block.在你的 catch 块中首先使用 console.log(err) 而不是 dispatch(authError(err.response.data)) 。 Then inspect in chrome and check what is the structure of err in Console.然后在chrome中检查并检查控制台中err的结构是什么。
export function authError(payload) {
return {
type: AUTH_FAILURE,
payload,
};
}
export function loginUser(creds) {
return (dispatch) => {
if (creds.email.length > 0 && creds.password.length > 0) {
axios
.post("/auth/login", creds)
.then((res) => {
const payload = res.data;
dispatch(receiveToken(payload));
dispatch(doInit());
dispatch(push("/app"));
})
.catch((err) => {
console.log(err);
//dispatch(authError(err.response.data));
});
} else {
dispatch(authError("Something was wrong. Try again"));
}
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.