Node.js and JSON.stringify missing some values/parameters from object
This is because Model.find() is returning a MongooseDocument. If you want a plain JavaScript object, which you can add properties to, you need to specify the "lean" option:
Model.find().lean().exec(function (err, thing) { thing.title = "some title"; console.log(JSON.stringify(thing));});
This will work fine, provided you don't need to save the thing object again (as this is not a MongooseDocument, no getters and setters are applied).
Since you mention mongoose it may be that that.thing
is a special object with some specific properties.
There are two things that could be affecting this.
Either thing
is actually a getter/setter with some horrible logic
Or more likely thing
has a .toJSON
method that writes the object to json and you havn't augmented or removed that method to make JSON.stringify
work.
My default JSON.stringify
calls the .toJSON
method if it exists.
So a simple console.log(that.thing.toJSON);
should identify your problem
Anyway what you really aught to be doing when you want to log data and make sure it logs what the current data is in a blocking fashion.
console.warn(util.inspect(that.thing));
You are seeing "function () { return this.toObject(); }" because toJSON is a prototyped function.
That is, console.log(that.thing.toJSON) will print the function itself but console.log(that.thing.toJSON()) will print the value returned from the function. Adding the extra brackets will execute the function.
Try console.log(that.thing.toJSON()) and see if you get what you are looking for.