我已经在后端设置了express,这是逻辑,如果帐户已经存在。
try {
//check if email already exists
const existingUser = await User.findOne({ email : email });
if (existingUser) {
// return res.send('Already exists')
return res.status(400).json({err: "Account with email already exists"});
};
我可以使用axios获取200/201的数据。。。 回应。 但这次不行。 对于400个代码,它还将json响应返回给POSTMAN,但不返回给axios。 我需要提取响应数据我的前端代码!
const submit = async (e) => {
e.preventDefault();
if(password !== password2){
setErr("Passwords do not match")
};
const userData = { name, email, password };
const url = "http://localhost:5000/api/users/signup";
const res = await axios.post(url, userData);
// NO OUTPUT
console.log(res.data);
console.log(res);
}
您需要一个try/catch
const submit = async e => {
e.preventDefault();
if (password !== password2) {
setErr("Passwords do not match");
}
const userData = { name, email, password };
const url = "http://localhost:5000/api/users/signup";
try {
const res = await axios.post(url, userData);
} catch (err) {
console.log(err.response);
}
};