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.

1
  • I tried searching for some event which is fired after an mongoose object is initialized, but did find any such thing. Commented Oct 10, 2012 at 17:39

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.