简体   繁体   English

我的捕获错误,我似乎无法弄清楚

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

相关问题 我似乎无法弄清文件夹结构,它说系统找不到我的文件? - I cant seem to figure out folder structure and it says the system cant find my file? 我试图为我的训练营做一个测验,但我不知道代码有什么问题 - im trying to make a quiz for my bootcamp and i cant figure out what is wrong with the code 我是一个新学习者,无法弄清楚为什么我的代码不起作用 - im a new learner and cant figure out why my code is not working 得到一个奇怪的jQuery错误,我无法弄清楚 - getting a weird jquery error that i cant figure it out 我添加了一个本地存储 function 但它似乎不适用于我创建的调度程序,我不知道为什么 - I have added a local storage function but it doesnt seem to work on my Scheduler i have created and i cant figure out why for 循环内部的 for 循环似乎无法弄清楚 - For loop inside of for loop cant seem to figure it out 我正在尝试添加用户提示以添加到我拥有的表中,但我无法弄清楚 - im trying to add a user prompt to add into the table i have but i cant figure it out 无法弄清楚为什么我在创建数据库表时出错 - Cant figure out why I'm getting an error creating DB tables 通过axios发布请求调用获取解析错误,我无法弄清楚 - Getting a parsing error with an axios post request call and i cant figure out 我的CSS问题似乎无法弄清楚 - Issue with my CSS that I can't seem to figure out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM