+ 5
Printing object's fields with JSON.stringify
I know that if I use JSON.stringify with an object It will obtain a string of all its fields excluding functions definition. When I tried to print the stringify of an exception object, it appears as an empty object, althought it has fields as message or lineNumber. What am I doing wrong ? What are the limits of stringify ? My test: https://code.sololearn.com/WOm5oG02878y/?ref=app
2 odpowiedzi
+ 7
Error has non-enumerable properties, thus cannot output it by JSON.stringify directly.
Check by:
var desc = Object.getOwnPropertyDescriptor(err, "message");
m(JSON.stringify(desc)) // { numerable: false }
Try to output by selecting its own property names manually:
m(JSON.stringify(err,Object.getOwnPropertyNames(err),3))
+ 6
Thank you so much Calvin !!