1

I want to have default values for certain fields in my mongoose models. The trick is I don't want to store these values in the database, but what to add them when the model is initialized.

Please help.

Pankaj
  • 2,538
  • 3
  • 26
  • 39
  • I tried searching for some event which is fired after an mongoose object is initialized, but did find any such thing. – Pankaj Oct 10 '12 at 17:39

2 Answers2

2

You can define an 'init' middleware function for the schema which runs when a model instance is loaded from the database. That should let you manipulate the instance to add your defaults as needed.

Also see this related question for more details as the docs are pretty spare on this.

Community
  • 1
  • 1
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
0

Maybe a json file? Using a require('path/to/json' ) you can acces to it and get the values. Im actually doing something like that, but using the same JSON SCHEMA wich is build for validation purpose.

Jonathan P. Diaz
  • 3,213
  • 1
  • 18
  • 13