Printing object's fields with JSON.stringify | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

29th Mar 2019, 10:38 PM
Javier Felipe Toribio
Javier Felipe Toribio - avatar
2 Answers
+ 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))
29th Mar 2019, 11:31 PM
Calviղ
Calviղ - avatar
+ 6
Thank you so much Calvin !!
30th Mar 2019, 9:01 AM
Javier Felipe Toribio
Javier Felipe Toribio - avatar