Skip to content

Commit

Permalink
docs(discriminator): add warning to always attach hooks before callin…
Browse files Browse the repository at this point in the history
…g discriminator()

Fix #5706
  • Loading branch information
vkarpov15 committed Nov 9, 2017
1 parent b4cf78f commit bb6a64e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions test/docs/discriminators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ describe('discriminator docs', function () {
* types are stored in the same document array (within a document) rather
* than the same collection. In other words, embedded discriminators let
* you store subdocuments matching different schemas in the same array.
*
* As a general best practice, make sure you declare any hooks on your
* schemas **before** you use them. You should **not** call `pre()` or
* `post()` after calling `discriminator()`
*/
it('Embedded discriminators in arrays', function(done) {
var eventSchema = new Schema({ message: String },
Expand All @@ -295,12 +299,15 @@ describe('discriminator docs', function () {

// The `events` array can contain 2 different types of events, a
// 'clicked' event that requires an element id that was clicked...
var Clicked = docArray.discriminator('Clicked', new Schema({
var clickedSchema = new Schema({
element: {
type: String,
required: true
}
}, { _id: false }));
}, { _id: false });
// Make sure to attach any hooks to `eventSchema` and `clickedSchema`
// **before** calling `discriminator()`.
var Clicked = docArray.discriminator('Clicked', clickedSchema);

// ... and a 'purchased' event that requires the product that was purchased.
var Purchased = docArray.discriminator('Purchased', new Schema({
Expand Down

0 comments on commit bb6a64e

Please sign in to comment.