[英]Not able to respond to custom error message sent from express
这个问题让我很恼火,因为我知道这与我没有正确理解这个问题有关——这使得很难找到答案,尽管花了几个小时阅读和尝试不同的事情。
我的问题/问题是这样的,当他们注册时,我正在将用户保存到 mongodb 数据库,我的架构不允许重复的电子邮件,并向我发回一个错误。 我可以在终端中控制台记录错误,但在将其发送回客户端时遇到问题。 或者我在处理它时遇到问题,如果它回来了,我不太确定在这两个步骤中我无法访问错误消息。
这是我保存用户的 POST 路线:
router.post('/users', (req, res) => {
let body = _.pick(req.body, ['email', 'password']);
let user = new User(body);
user.save().then(() => { // this all works and will save the user, if there are no errors
return user.generateAuthToken();
}).then((token) => {
res.header('Authorization', `Bearer ${token}`).send(user);
}).catch((err) => { // This is where my problem is
console.log(err); // This will log the mongodb error here, about duplicate emails
res.status(500).send(err); // I'm trying to send the mongodb error message back to the client to display it on the screen (I will handle making the message friendly to read, once I can get this to work)
});
});
所以我的捕获是得到 mongo 错误,然后我尝试通过将其发送给客户端来响应它。
这是我的客户端代码:
axios({
method: 'post',
url: '/auth/users',
headers: {
'Content-Type': 'application/json'
},
data: {
email,
password
}
}).then((res) => {
console.log('this is the response', res);
if (res.status === 200) {
var authToken = res.headers.authorization.split(' ')[1];
authenticateUser(authToken);
this.props.history.replace('/dashboard');
} // This all works fine for a signup with no errors
}).catch((err) => {
console.log('Signup error:', err);
// I am expecting the above line of code to log the long Mongodb
// error message that I am sending back in my res.status(500).send(err)
// catch call from the server, but instead all I am getting is
// "Signup error: Error: Request failed with status code 500"
});
要么我没有正确发送错误,要么当它返回时我没有正确处理它,但我不知道它是什么或为什么。
我什至无法发回res.status(500).send('some string here')
并访问该字符串。
谢谢
所以我刚刚检查了 postman,通过发送可能导致错误的 POST,我收到了正确的响应。
我的服务器捕获实际上如下所示:
.catch((err) => {
res.status(500).send({message: err.message});
});
postman 响应体如下所示:
{
"message": "E11000 duplicate key error collection: authBoilerplate.users index: email_1 dup key: { : \"email@example.com\" }"
}
所以我只是没有在我的客户端代码中正确处理它,但仍然不知所措。
谢谢大家,我能够找到我的问题的答案,所以我在这里发布它,希望它可以帮助其他人。
我肯定是在发回我的自定义错误消息,只是我没有在客户端正确处理它。
当我在客户端上使用 catch 调用并记录错误时,我希望看到错误中包含的所有内容。 事实证明,该错误带有response
属性error.response
,这就是所有消息传递的地方。
因此,将我的 catch 调用更改为:
axios(//... send post in here)
.then(// ... same as in my question)
.catch((err) => {
console.log('error', err);
console.log('error response', err.response); // this is where the actual error response message is error.response.message
});
导致记录堆栈跟踪和错误响应:
error Error: Request failed with status code 500
at createError (eval at <anonymous> (bundle.js:541), <anonymous>:16:15)
at settle (eval at <anonymous> (bundle.js:847), <anonymous>:18:12)
at XMLHttpRequest.handleLoad (eval at <anonymous> (bundle.js:520), <anonymous>:77:7)
error response Object {data: Object, status: 500, statusText: "Internal Server Error", headers: Object, config: Object…}
我仍然希望能够通过仅记录错误来查看我可以访问该“响应”属性,因此,如果有人对此有任何见解,最好将其包含在评论中。
解决此问题的另一种方法是将错误转换为字符串。
.catch((err) => {
res.status(500).send(err.toString());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.