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 :(
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.
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.
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.
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 })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With