简体   繁体   中英

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(). 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. JSON.stringify only considers (own) enumerable properties and message is not enumerable:

> Object.getOwnPropertyDescriptor(new Error('foo'), 'message');
Object {value: "foo", writable: true, enumerable: false, configurable: true}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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