gpt4 book ai didi

javascript - Axios 响应未定义

转载 作者:行者123 更新时间:2023-12-05 08:25:21 25 4
gpt4 key购买 nike

在 axios post req 之后显示服务器身份验证响应时我遇到了一个小问题。我正在尝试控制台日志服务器响应错误消息:“请在提交表单后填写所有必填字段。控制台一直显示“未定义”。在服务器端一切正常,网络选项卡显示正确的响应。

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

最佳答案

你们都在使用 async/await 等待响应并使用 promise 链 .post().then().catch()

试试这个:

axios.post("http://localhost:5000/auth/login", loginData)
.then((result) => {
console.log(result.data);
await getLoggedIn();
history.push("/player");
})
.catch(err => {
console.log(err)
})

关于javascript - Axios 响应未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66340992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com