【发布时间】:2020-11-29 02:37:46
【问题描述】:
我试图捕捉我在后端创建的自定义错误,但 Axios 和 fetch 没有得到 res.status(401).json({msg:"custom error name"})
我一直在收到 catch 的响应,但没有错误,没有 msg
try {
const loginUser = { email: user.email, password: user.password };
const loginRes = await Axios.post(
"http://localhost:4000/snackhub/login",
loginUser
);
localStorage.setItem("auth-token", loginRes.data.token);
dispatch(fetchUsers());
history.push("/");
} catch (err) {
console.log(err.response);
err.response.data.msg && console.log(err.response.data.msg); //hopefully i can display this thhe custom error from the backend
}
以下是后端错误示例:
const { error } = await logInValidation(req.body);
if (error) return res.status(400).json({ msg: error.message[0].details }); //is there something wrong in my syntax here?
const userExists = await Customer.findOne({ email: email });
if (!userExists)
return res.status(400).json({ msg: "email is not registered" });
【问题讨论】:
标签: javascript node.js reactjs axios fetch