Sam Sam - 4 months ago 14
Node.js Question

Clarification on a console.log of a Javascript object

This seems to be basic but I am missing something. I am using the node.js formidable module and am console logging the error as in:

uploadProc.on('error', function(err) {
console.log("Form upload error...");
console.log(typeof err);

The output is:

[Error: Request aborted]

As I see, it is neither an array nor a regular object with the property Error. If I want to stringify it, whatever type it is, how do I do that? May be it is string to begin with. But if that was the case, typeof would have returned "string." I check this at Mozilla. Seems to fall in the "any other object" category. Can someone explain what type of an object that is console logged as [Error: Request aborted] is?


It's an Error object:

> new Error('hello world') instanceof Error

It has a .toString() method that you can use to stringify it:

> console.log(new Error('hello world').toString())
Error: hello world

Or, if you want to output the stack trace:

> console.log(new Error('hello world').stack)
Error: hello world
    at repl:1:13
    at REPLServer.defaultEval (repl.js:262:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:431:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:211:10)
    at REPLServer.Interface._line (readline.js:550:8)
    at REPLServer.Interface._ttyWrite (readline.js:827:14)