简体   繁体   English

未处理的拒绝 (TypeError):error.response 未定义

[英]Unhandled Rejection (TypeError): error.response is undefined

When submitting form I get the following following error提交表单时出现以下错误

Unhandled Rejection (TypeError): error.response is undefined

handleLogin/<
D:/project3.0/containerapp/src/login.js:74

71 |     props.history.push('/dashboard');

72 |   }).catch(error => {

73 |     setLoading(false);

> 74 |     if (error.response.status === 401) setError(error.response.data.message);

|^75 |     else setError("Something went wrong. Please try again later.");
76 |   });

Here is my code:这是我的代码:

import React, { useState} from 'react';
import axios from 'axios';
import { setUserSession } from './Utils/Common';

function Login(props) {
  const [loading, setLoading] = useState(false);
  const username = useFormInput('');
  const password = useFormInput('');
  const [error, setError] = useState(null);
 
  // handle button click of login form
  const handleLogin = () => {
    setError(null);
    setLoading(true);
    axios.post('http://localhost:4000/users/signin', { username: username.value, password: password.value }).then(response => {
      setLoading(false);
      setUserSession(response.data.token, response.data.user);
      props.history.push('/dashboard');
    }).catch(error => {
      setLoading(false);
      if (error.response.status === 401) setError(error.response.data.message);
      else setError("Something went wrong. Please try again later.");
    });
  }
 
  return (
    <div>
      Login<br /><br />
      <div>
        Username<br />
        <input type="text" {...username} autoComplete="new-password" />
      </div>
      <div style={{ marginTop: 10 }}>
        Password<br />
        <input type="password" {...password} autoComplete="new-password" />
      </div>
      {error && <><small style={{ color: 'red' }}>{error}</small><br /></>}<br />
      <input type="button" value={loading ? 'Loading...' : 'Login'} onClick={handleLogin} disabled={loading} /><br />
    </div>
  );
}
 
const useFormInput = initialValue => {
  const [value, setValue] = useState(initialValue);
 
  const handleChange = e => {
    setValue(e.target.value);
  }
  return {
    value,
    onChange: handleChange
  }
}
 
export default Login;

Obviously - the error object doesn't contain the response field.显然 - error object 不包含response字段。 In the context of using axios - that basically means that the request is not performed properly (and there's no response from the server at all)在使用axios的上下文中 - 这基本上意味着请求没有正确执行(并且根本没有来自服务器的响应)

From this answer:这个答案:

A conventional approach is to catch errors in the catch() block like below:一种传统的方法是在catch()块中捕获错误,如下所示:

axios.get('/api/xyz/abcd')
  .catch(function (error) {
    if (error.response) {
      // Request made and server responded
      console.log(error.response.data);
      console.log(error.response.status);
      console.log(error.response.headers);
    } else if (error.request) {
      // The request was made but no response was received
      console.log(error.request);
    } else {
      // Something happened in setting up the request that triggered an Error
      console.log('Error', error.message);
    }

  });

暂无
暂无

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

相关问题 error.response 在 catch axios 中未定义 - error.response is undefined in catch axios 来自 api 的用户搜索得到错误的过滤响应:`未处理的拒绝(TypeError):无法读取未定义的属性'结果' - Filter response from api on user search getting error :`Unhandled Rejection (TypeError): Cannot read property 'results' of undefined` [未处理的承诺拒绝:类型错误:未定义不是对象(评估“response.data”)] - [Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'response.data')] 错误:'未处理的 Promise 拒绝:TypeError:无法读取未定义的属性 - ERROR: 'Unhandled Promise rejection: TypeError: Cannot read properties of undefined axios:未处理的拒绝(TypeError):无法读取未定义的属性“错误” - axios: Unhandled Rejection (TypeError): Cannot read property 'error' of undefined 未处理的拒绝 (TypeError):无法读取未定义的属性(读取“错误”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'error') 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) 反应:未处理的拒绝(TypeError):无法读取未定义的属性(读取“错误”) - React : Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'error') 放入新的未定义对象Error(error.response); - Undefined object when putting in throw new Error(error.response);
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM