【问题标题】:Cannot extract error message from error.message无法从 error.message 中提取错误消息
【发布时间】:2023-04-03 09:19:01
【问题描述】:

我的 express 应用中有这行代码:

 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

如何访问原始按摩?

【问题讨论】:

    标签: node.js express error-handling axios


    【解决方案1】:

    您不想简单地捕获错误,500 错误只是 500 错误(带有它自己的通用消息)。

    您需要从响应正文中提取您在响应中发送的消息。这是来自 axios https://github.com/axios/axios/issues/960 的 github 问题页面:

    axios
    .post('ajax/register/otp', this.registerData)
    .then(function (response) {
           ...
    })
    .catch(function (error) {
          console.log(error.response);
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-04
      • 2017-10-01
      • 2016-08-06
      • 1970-01-01
      • 2020-09-10
      相关资源
      最近更新 更多