.toJSON()
Whenever Waterline retrieves a record, it checks whether or not the record’s model has a customToJSON
method defined; if so, Waterline adds the method to the record as its toJSON
property. toJSON
is not intended to be called directly in your code. Instead, it is used automatically when a record is serialized via a call to JSON.stringify()
. The res.json()
method, in particular, stringifies objects in this way.
When a customToJSON
is defined for a model, the .toJSON()
method will be added to records retrieved via .find()
, .findOne()
, .findOrCreate()
and .stream()
, as well as those retrieved by setting the fetch
meta key to true
in calls to .create()
, .createEach()
, .update()
and .destroy()
. If any child records are attached via .populate()
, and the corresponding models have customToJSON
methods, then the child records will also have .toJSON()
functions attached.
See the customToJSON documentation for more info on how to customize the way your records are presented.