2

I have a mongodb database and I am using mongoose with nodejs. When I console a mongoose object, I get the usual keys that I have defined in my schema.

console.log(mongooseObject); 
/*
{ 
  _id: 5b8b7d627defb42e3f6349ef,
  name: 'John Doe',
}
*/

But when I tried to console it's keys using Object.keys(), it prints 5 keys that are not really the part of my schema and must being used internally by mongoose for various operations.

console.log(Object.keys(mongooseObject)) // [ '$__', 'isNew', 'errors', '_doc', '$init' ]

My question is, how does console.log determines what part of the object to be printed? When just logging the object directly, how does it work its way through these keys and their values to determine what to print?

Is there a toString() like function that a class defines which describes the JSON representation of an object? Something which is then used by console.log to actually print.

Edit: I'm getting a lot of mongoose related solutions suggessting me to use .toJSON() and .toObject(). Perhaps I should have made it clear that I'm not really looking for mongoose related stuff. I just used it's example because that's how I notice such peculiar behaviour. I'm just generally intereted in how javascript is working behind here. And can I write a Class where I can describe how an object being instanciated from it should be consoled?

  • As far as I know javascript behaviors, probably the "mongooseObject" has a custom toString function. – Ramin Dec 03 '18 at 14:57

3 Answers3

2

Mongoose Object is a very complex object with lots of functions inside. If you want to just convert the object the the pure mongo json document you can use toJSON() function, then you can easily use .keys.

 console.log(Object.keys(mongooseObject.toJSON()))
Babak
  • 615
  • 6
  • 15
0

I don't think this is the console printing different keys than the keys in the object at that moment. The reason you do get the objected printed correctly the first time is because the object you pass console.log is being read the moment you are looking at it, but if you were to log JSON.parse(JSON.stringify(mongooseObject)) you would probably come to the realization the object is empty at that moment

The reason Object.keys returns different results is because it will create an array out of the keys in that object the moment its called.

Ideally, you just wait for the object to actually be there before printing its keys. To help you with that, we would need more code.

Objects yield this behavior because they are passed by reference, read this stackoverflow post for excellent information on what that means.

realappie
  • 4,656
  • 2
  • 29
  • 38
0

It can print anything it wants.

console.log is actually not part of the Ecmascript language standard. While there is a WhatWG standard for the console API, a JavaScript implementation isn't required to implement it at all (although AFAIK all of them do), and even in that standard parts are explicitly implementation-defined.

So the short answer is that there isn't an answer. The longer answer is that it's almost certainly going to print keys, possibly internal slots like [[class]], etc. but it's going to depend greatly on the actual object you pass in.

Jared Smith
  • 19,721
  • 5
  • 45
  • 83