简体   繁体   English

我的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

i am trying to access values in my express response error. 我正在尝试访问我的快速响应错误中的值。 When I console.log the error like this... 当我console.log错误是这样的...

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

The console looks like the following 控制台如下所示

[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"}']

For Example; 例如; how do i access err.title so i can send this to the client. 我如何访问err.title以便将其发送给客户端。

reference this question for more Parse Error response in Express NodeJS app 请参考此问题以获取Express NodeJS应用中的更多解析错误响应

If I'm not wrong, express uses a generic error object defined in the standard , and there are only three properties: 如果我没看错,express使用standard中定义的通用错误对象,并且只有三个属性:

Error.prototype.constructor Error.prototype.constructor
Specifies the function that created an instance's prototype. 指定创建实例原型的函数。
Error.prototype.message 错误原型消息
Error message. 错误信息。
Error.prototype.name Error.prototype.name
Error name. 错误名称。

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. 一些库(例如mongoose)使用自定义错误对象,因此您可以访问更多错误详细信息(和属性),如果是这种情况,则可以尝试在的官方文档中查找“错误处理”一章。该库,并查看错误对象中是否有更多详细信息。 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: 在这种情况下,我认为您不会使用express提供的err对象,而是使用库方法提供的err ,这会引起错误,有点像:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM