question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documentation: clarify the difference between toObject() and toJSON()

See original GitHub issue

What is the difference between these two? It’s not immediately apparent which I should be targeting when I use a transform. Up until now I have just blindly added the same transform on both.

My use case: I want to be able to modify the json that is sent to the front end (through express), but I don’t want this modified data to be persisted to the database whenever a save() is called.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6

github_iconTop GitHub Comments

14reactions
pritchardtwcommented, Apr 9, 2019

To all y’all finding this in 2019…

JSON.stringify checks objects passed to it for a function called toJSON. If it exists it uses that function to determine the object that should be stringified. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON()_behavior

If you are using express, when calling res.send(object) and the client accepts JSON it will JSON.stringify the object passed into send. This means when you pass a mongoose model directly into res.send(model) it will send the toJSON format. If you just need to get an object representation of your model then I would use toObject.

If you ever have a use case where you want your models turned into objects in two different ways, then it makes sense to have both. If you want to be able to easily res.send(model) you need toJSON, and if you ever want to toObject that produces a result other than toJSON you need that to run with it’s own options.

toJSON seems to essentially just exist for compatibility with utilities that already call toJSON such as JSON.stringify.

0reactions
fitzhaveycommented, Oct 14, 2020

A difference between toJSON and toObject I’ve found is that properties on the schema defined as a map seem to be set to an empty object when calling toObject.

I’m not sure if this behaviour is intended or not but makes toJSON much more useful in the case the record contains a map.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the difference between Mongoose toObject and ...
toJSON is exactly the same as the toObject option but only applies when the document's toJSON method is called. Is toJSON an alias...
Read more >
Mongoose toObject and toJSON transform behavior with sub ...
So it looks like toObject is being applied to sub-documents while toJSON is not. And this is different from what the documentation says:....
Read more >
Mongoose v6.8.1: Document
«Boolean,Document» whether mongoose thinks this doc is deleted. ... There is one difference between toJSON() and toObject() options. When you call toJSON() ...
Read more >
Working with JSON - Learn web development | MDN
Converting between objects and text · parse() : Accepts a JSON string as a parameter, and returns the corresponding JavaScript object. · stringify ......
Read more >
json — JSON encoder and decoder — Python 3.11.1 ...
The other arguments have the same meaning as in load() . If the data being deserialized is not a valid JSON document, a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found