我的EXPRESS/NodeJs响应错误中无法访问值[String:'Error:Request return error code:404 [英] Cant access values in my EXPRESS / NodeJs response error [String: 'Error: Request returned error code: 404

查看:26
本文介绍了我的EXPRESS/NodeJs响应错误中无法访问值[String:'Error:Request return error code:404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问我的快速响应错误中的值.当我console.log出现这样的错误...

i am trying to access values in my express response error. When I console.log the error like this...

app.use(function(err, req, res, next){
    console.log(err)
});

控制台如下所示

[String: 'Error: Request returned error code: 404 and body: {"status":404,"title":"No option(s) were found with this query.","type":"https://developer.bigcommerce.com/api#api-status-codes"}']

例如;我如何访问 err.title ,以便可以将其发送给客户端.

For Example; how do i access err.title so i can send this to the client.

请参考此问题以获取更多的在Express NodeJS应用中解析错误响应

reference this question for more Parse Error response in Express NodeJS app

推荐答案

如果我没记错,express使用

If I'm not wrong, express uses a generic error object defined in the standard, and there are only three properties:

Error.prototype.constructor
指定创建一个实例的原型.
Error.prototype.message
错误信息.
Error.prototype.name
错误名称.

Error.prototype.constructor
Specifies the function that created an instance's prototype.
Error.prototype.message
Error message.
Error.prototype.name
Error name.

某些库(例如猫鼬)使用自定义错误对象,因此您可以访问更多错误详细信息(和属性),如果您遇到这种情况,则可以尝试在错误处理"一章中查找错误处理"一章.该库的官方文档,并查看您是否在错误对象中有更多详细信息.在这种情况下,我认为您不会使用express提供的 err 对象,而是使用库方法提供的 err 对象,该对象会引起错误,例如:

Some libraries (like mongoose for example) use a custom error object, so you can access to more error details (and properties), if this is your case, you could try to look for an "Error handling" chapter in the official docs of that library, and see if you have more details in the error object. In this cases I think you won't use the err object provided by express but an err provided by the library method which rises the error, somewhat like:

...
app.use((err, req, res, next)=>{
 library.method((err1, data)=>{
  console.log(err1);// use this error object
 })
})
... 

这篇关于我的EXPRESS/NodeJs响应错误中无法访问值[String:'Error:Request return error code:404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆