SchemaType.cast()

Parameters
  • caster «Function|false» Function that casts arbitrary values to this type, or throws an error if casting failed
Returns:
  • «Function»

Get/set the function used to cast arbitrary values to this particular schematype instance. Overrides SchemaType.cast().

Example:

  1. // Disallow `null` for numbers, and don't try to cast any values to
  2. // numbers, so even strings like '123' will cause a CastError.
  3. const number = new mongoose.Number('mypath', {});
  4. number.cast(function(v) {
  5. assert.ok(v === undefined || typeof v === 'number');
  6. return v;
  7. });