console.log returns [Object] when object is in multiple other objects

Viewed 1457

When I have a lot of objects together and I run it using nodejs in cmd It says the [Object] instead of the actual contents of the object.

here is an example from my command prompt example of problem

myObject = {
  '698045139763069009':{ users: { '560433156377804828': {name: "mark", age: "28"}},info: {} }
}
console.log(myObject);

code here ^^^

same thing happens with an array

myObject = {
  '698045139763069009':{ users: { '560433156377804828': ["mark", 28]},info: {} }
}
console.log(myObject);

example

and a screen shot ^^^

help would be appreciated! thanks

4 Answers

It's to make the output more readable.

If you want to print the whole object use util.inspect() as described here or use JSON.stringify().

Looks like your console doesn't print nested objects.

Change console.log(someObject) to console.log(JSON.stringify(someObject)).

Note that it will give error if the object is cyclic / has circular references.

Turns out my console doesn't print nested arrays or objects. Thanks for all the help!

It's to make it more readable when working with large amounts of nested arrays and objects.

I think the answer to this (and which uses util.inspect()), that will also display nested objects, is console.dir().

Consider below example:

  {
    fieldName: 'price_point',
    value: [ 'priced at 25 dollars' ],
    entityValue: [ [Object] ] <----- (this annoying thing here)
  }

TURNS THIS INTO

{
  fieldName: 'price_point',
  value: [ 'priced at 25 dollars' ],
  entityValue: [ { number: 25, units: 'Dollar' } ] <--- (to this!)
}

JSON.stringify() DOES NOT show nested arrays and objects.

Related