[英]Why is a Javascript Error object not treated as an object by expressjs
I have a node/express backend. 我有一个节点/表达后端。 In my backend I create an error object if there is an error, but when I try to send it in the response it is not there.
在后端,如果有错误,我会创建一个错误对象,但是当我尝试在响应中发送它时,它就不存在了。 I was able to make my code work by building a new object in the response, but I would like to know why it didn't work.
我可以通过在响应中构建新对象来使代码正常工作,但是我想知道为什么它不起作用。
The relevant code is: 相关代码为:
var error = new Error('some error message')
app.send({error}) // returns {}
app.send({error: error.message}) // returns {error: 'some error message}
According to MDN docs, an error object is an object, and we should therefore be able to pass it directly into app.send(). 根据MDN文档,错误对象是一个对象,因此我们应该能够将其直接传递到app.send()中。 This didn't work in practice, and I would like to be able to explain why.
这在实践中不起作用,我想能够解释原因。 Thanks for your help!
谢谢你的帮助!
I assume that app.send
converts the value to JSON. 我假设
app.send
将值转换为JSON。 JSON.stringify
only considers (own) enumerable properties and message
is not enumerable: JSON.stringify
仅考虑(自己的) 可枚举属性,而message
则不可枚举:
> Object.getOwnPropertyDescriptor(new Error('foo'), 'message');
Object {value: "foo", writable: true, enumerable: false, configurable: true}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.