Axios response undefined

I have a little problem while displaying server auth response after axios post req. I'm trying to console log server response errorMessage: "please all required fields after form submit. Console keeps showing "undefined". On the server side everything is fine, the network tab shows correct response.

const Login = () => {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [errors, setErrors] = useState("");

  const { getLoggedIn } = useContext(AuthContext);
  const history = useHistory();
  async function login(e) {
    e.preventDefault();
    try {
      const loginData = {
        email,
        password,
      };

      const res = await axios
        .post("http://localhost:5000/auth/login", loginData)
        .then((result) => {
          console.log(result.data);
        });

      await getLoggedIn();

      history.push("/player");
    } catch (err) {
      console.log(err.data);
    }
  }

  return (
    <div className="authWrapper">
      <form className="box" onSubmit={login}>
        <h1>LOGIN</h1>
        <input
          autoComplete="off"
          className="input"
          type="email"
          placeholder="Email"
          onChange={(e) => setEmail(e.target.value)}
          value={email}
        />
        <input
          autoComplete="off"
          className="input"
          type="password"
          placeholder="Password"
          onChange={(e) => setPassword(e.target.value)}
          value={password}
        />
        <button className="submit" type="submit">
          {`Log in${" "}`}
          <FontAwesomeIcon icon={faArrowRight}></FontAwesomeIcon>
        </button>

        <Link to="/register">
          <button className="changeForm">
            {`Register${" "}`}
            <FontAwesomeIcon icon={faSync}></FontAwesomeIcon>
          </button>
        </Link>
      </form>
    </div>
  );
};

export default Login;
<div></div>;

console error

Network login endpoint response



Read more here: https://stackoverflow.com/questions/66340992/axios-response-undefined

Content Attribution

This content was originally published by Vasper at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: