Sunday, 12 March 2017

Mongoose schema transform field before returning the record

Is it possible to add a default value if the field is empty/not present in the record?

ex:

if ( ! record.options ) { record.options = {}; }

After searching a lot, the response i found was i can do this using .toObject() or .toJSON(), But i am not calling any of these and i just want to add this conditions on the schema so it works directly.

Right now i am checking for this condition in the returned record/(s) which is bad as i am repeating the same logic for .find and .findOne



via DJ 01

No comments:

Post a Comment