【问题标题】:ExpressJS: The custom error module does not recognize this errorExpressJS:自定义错误模块无法识别此错误
【发布时间】:2017-11-20 07:48:16
【问题描述】:

我正在运行 Express.JS 后端,如果用户尝试使用已在使用的电子邮件进行注册,则将其设置为返回 422 错误和自定义消息,如下所示:

const existingUser = await User.findOne({ email: email });
    if (existingUser) {
        return res.status(422).send({ error: 'Email is already in use' });
    }

但是,我在前端看到的响应是这样的:

"response":{"data":"The custom error module does not recognize this error.","status":422,"statusText":"Unprocessable Entity"

显然,有些东西正在改变通往前端的错误。

我现在在本地运行它进行开发,但生产部署到 Azure,所以我有一个 Web.config 文件和一个 iisnode.yml 文件。我已将此代码添加到前者,以确保它不会导致问题:

<system.webServer>
  <httpErrors existingResponse="PassThrough">
  </httpErrors>
</system.webServer>

我该如何解决这个问题?

【问题讨论】:

  • 这个问题是否与expressjs有关,如何?您收到的消息似乎来自某些 IIS。你读过这个吗,已经:stackoverflow.com/questions/12908764/…
  • 是的,阅读它并将建议的代码添加到 Web.config。问题依然存在。
  • 打扰了,再问你一句:这个问题和快递有什么关系?
  • 我在后端使用 Express 并从那里返回错误。我不知道为什么错误会在传递到前端的过程中被转换。

标签: node.js azure express iisnode


【解决方案1】:

在设置&lt;httpErrors&gt; 后它对我有用,如下所示:

浏览器输出:

【讨论】:

  • 是的,我从这条线开始,它似乎没有什么不同。
  • 尝试重启应用服务并清除浏览器缓存。
【解决方案2】:

不确定,但以下应该可以。

return res.status(422).send('Email is already in use');

【讨论】:

  • 不,同样的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
  • 2020-11-03
  • 1970-01-01
  • 2015-07-25
  • 2014-07-26
  • 2016-01-30
  • 1970-01-01
相关资源
最近更新 更多