How to Get the Full Object in Node.Js'S Console.Log(), Rather Than '[Object]'

How can I get the full object in Node.js's console.log(), rather than '[Object]'?

You need to use util.inspect():

const util = require('util')

console.log(util.inspect(myObject, {showHidden: false, depth: null, colors: true}))

// alternative shortcut
console.log(util.inspect(myObject, false, null, true /* enable colors */))

Outputs

{ a: 'a',  b: { c: 'c', d: { e: 'e', f: { g: 'g', h: { i: 'i' } } } } }

How to print the full data coming in [object Object] in nodejs

By default, the objects are not logged deeply. Probably because could exist circular properties. When you write:

console.log(`Loaded ${params.response}`) // Loaded [object Object]

Its similar to:

console.log(params.response.toString()) // [object Object]

Instead, use JSON.stringify():

const object = {property: 'value'}

console.log(object.toString()) // [object Object]
console.log(JSON.stringify(object)) // {property: "value"}

Also you can specify the format of the output format for better reading:

console.log(JSON.stringify(object), null, 2) // Format with 2 spaces identation

// {
// "property": "value"
// }

How can I display a JavaScript object?

If you want to print the object for debugging purposes, use the code:

var obj = {
prop1: 'prop1Value',
prop2: 'prop2Value',
child: {
childProp1: 'childProp1Value',
},
}
console.log(obj)

will display:

screenshot console chrome

Note: you must only log the object. For example, this won't work:

console.log('My object : ' + obj)

Note ': You can also use a comma in the log method, then the first line of the output will be the string and after that, the object will be rendered:

console.log('My object: ', obj);

How to log a deeply nested Object as it is in node console

You can use JSON.stringify() to print the entire object as a string. Then, it will not print as c: [Array]. Instead it will print all the properties and values no matter how deeply it is nested.

console.log(JSON.stringify(a));

Can't access object property, even though it shows up in a console log

The output of console.log(anObject) is misleading; the state of the object displayed is only resolved when you expand the Object tree displayed in the console, by clicking on >. It is not the state of the object when you console.log'd the object.

Instead, try console.log(Object.keys(config)), or even console.log(JSON.stringify(config)) and you will see the keys, or the state of the object at the time you called console.log.

You will (usually) find the keys are being added after your console.log call.

How console.log(object) works in background?

As you can see in Node.js console.log documentation, it uses, behind the scenes, util.format to format its output.

According to util.format documentation, it returns a string representation of an object with generic JavaScript object formatting, similar to util.inspect.

You can see its actual implementation, at least for Node.js, here: https://github.com/nodejs/node/blob/75dc8938a40100a53323ed87159a1ab2f149ceca/lib/internal/util/inspect.js#L1567. Have fun reading this code :)

How to show full object in Chrome console

Use console.dir() to output a browse-able object you can click through instead of the .toString() version, like this:

console.dir(functor);

Prints a JavaScript representation of the specified object. If the object being logged is an HTML element, then the properties of its DOM representation are printed [1]


[1] https://developers.google.com/web/tools/chrome-devtools/debug/console/console-reference#dir



Related Topics



Leave a reply



Submit