Document.prototype.$locals

Type:
  • «property»

Empty object that you can use for storing properties on the document. This is handy for passing data to middleware without conflicting with Mongoose internals.

Example:

  1. schema.pre('save', function() {
  2. // Mongoose will set `isNew` to `false` if `save()` succeeds
  3. this.$locals.wasNew = this.isNew;
  4. });
  5. schema.post('save', function() {
  6. // Prints true if `isNew` was set before `save()`
  7. console.log(this.$locals.wasNew);
  8. });