无法从error.message中提取错误消息
我的快速应用程序中有以下代码行:
catch (err) {
res.status(500).send(err.message);
}
当我控制台记录错误时,我收到此消息:
name: 'TokenExpiredError',
message: 'jwt expired',
但是当我使用axios请求在客户端接收到错误时,如下所示:
catch (err) {
console.log(err.message)
我明白了:Request failed with status code 500
如何获得原始按摩?
回答如下:您并不想简单地捕获错误,500错误只是500错误(带有其自己的通用消息)。
您需要从响应正文中提取在响应中发送的消息。这是来自axios https://github/axios/axios/issues/960的github问题页面:
axios
.post('ajax/register/otp', this.registerData)
.then(function (response) {
...
})
.catch(function (error) {
console.log(error.response);
});