【发布时间】:2021-10-08 08:16:59
【问题描述】:
node js 后端中的错误处理中间件:
app.use((error, req, res, next) => {
console.log(error);
const status = error.statusCode || 500;
const message = error.message;
const data = error.data;
res.status(status).json({ message: message, data: data });
});
我的应用中有以下 try catch 块:
userLogin() {
//axios vue instance
this.$http
.post("/auth/signup", this.formData)
.then((res) => {
// res.status == 201 ? (this.formData = {}) : "";
console.log(res);
})
.catch((err) => {
console.log(err);
console.log(err.data.data);
console.log(err.data.msg);
});
},
上述catch块的输出如下: [![在此处输入图片说明][2]][2]
当我的 rest api 以以下格式发送消息时(见网络 > 预览)
{
message: "Validation failed.",
…
}
data: [{
location: "body",
param: "email",
value: "test@test.com",
msg: "E-Mail address already exists!"
}]
message: "Validation failed."
我想访问数据数组并打印它的内容。
如何访问data?
【问题讨论】:
标签: javascript node.js vue.js error-handling axios