Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to console.log large object? [duplicate]

I'm trying to see what the object contains

With console.log(obj) the console cuts off from the lines and I can't see the entire structure.

Then I tried writing it to a file

fs.writeFile('test', JSON.stringify(obj));

But I get some error about circular references.

Is there any other way to view the object lol? The object is a "connection" from the nodejs websocket module. The docs are very poor and I can't seem to find the property that holds the IP address :(

like image 289
thelolcat Avatar asked Jan 26 '15 17:01

thelolcat


People also ask

How do I console log a whole JSON object?

The simplest way is using the Console log() method to log objects as JSON in JavaScript. The console. log() method called with an object or objects as arguments will display the object or objects.

What is the difference between console log and console dir?

The main difference between these two methods is that the console. log() method displays the “toString” representation of any object passed to it. Whereas, the console. dir() method displays an interactive list of the properties of the specified JavaScript object.

What is object object in JS?

An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method. Objects in JavaScript, just as in many other programming languages, can be compared to objects in real life.


1 Answers

fs.writeFile('test', JSON.stringify(obj));

But I get some error about circular references.

That's what happens with JSON. JSON also ignores functions, regexes, dates, etc.

You can use util.inspect, which is what Node's console.log() uses internally to get the entire object, which can then be written to a file:

var util = require('util');
fs.writeFileSync('test.txt', util.inspect(obj));

And if you want infinite depth, and hidden properties:

util.inspect(obj, { showHidden: true, depth: null })
like image 140
Scimonster Avatar answered Sep 20 '22 04:09

Scimonster