When debugging using console.log()
, how can I get the full object?
const myObject = {
\"a\":\"a\",
\"b\":{
\"c\":\"c\",
\"d\":
You can also do
console.log(JSON.stringify(myObject, null, 3));
let myVar = {a: {b: {c: 1}}};
console.log(JSON.stringify( myVar, null, 4 ))
Great for deep inspection of data objects. This approach works on nested arrays and nested objects with arrays.
You can simply add an inspect()
method to your object which will override the representation of object in console.log
messages
eg:
var myObject = {
"a":"a",
"b":{
"c":"c",
"d":{
"e":"e",
"f":{
"g":"g",
"h":{
"i":"i"
}
}
}
}
};
myObject.inspect = function(){ return JSON.stringify( this, null, ' ' ); }
then, your object will be represented as required in both console.log and node shell
A simple trick would be use debug
module to add DEBUG_DEPTH=null
as environment variable when running the script
Ex.
DEBUG=* DEBUG_DEPTH=null node index.js
In you code
const debug = require('debug');
debug("%O", myObject);
Another simple method is to convert it to json
console.log('connection : %j', myObject);