1

I'm trying to convert object to string using JSON.stringify and I get empty object

console.log('typeof',typeof e,' e value is',e, 'JSON stringify is',JSON.stringify(e))

the error message when I try to print

typeof object e value is Error: Error: A network error (such as timeout, interrupted connection or unreachable host) has occurred. JSON stringify is {}

enter image description here

5
  • 1
    @ManuallyOverridden console.log is variadic — you can pass multiple objects, which are separated by a ,. Commented Jul 1, 2018 at 6:38
  • It looks like e is an error object. In node, stringifying an error results in {}. Not sure how you are running your code. Commented Jul 1, 2018 at 6:40
  • @ManuallyOverridden even I do let a = JSON.stringify(e) and I print a I get empty object.. Commented Jul 1, 2018 at 6:40
  • @Mark_M how can I print error object? I want use it to string and show in client side Commented Jul 1, 2018 at 6:45
  • See this answer stackoverflow.com/questions/18391212/… Commented Jul 1, 2018 at 6:47

1 Answer 1

3

Your object e is an error object. When you try to stringify that you get {} in chrome and node. Safari shows a little more info.

let e = new Error("hello")
console.log(typeof e)
console.log(JSON.stringify(e))

You can test for errors with:

let e = new Error("Some error happened")
if (e instanceof Error) {
  console.log("Error:", e.message)
 }

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.