Mongoose v8.14.1: Error (original) (raw)


Error()

Parameters:
Type:
Inherits:

MongooseError constructor. MongooseError is the base class for all Mongoose-specific errors.

Example:

const Model = mongoose.model('Test', new mongoose.Schema({ answer: Number }));
const doc = new Model({ answer: 'not a number' });
const err = doc.validateSync();

err instanceof mongoose.Error.ValidationError; // true

Error.CastError

Type:

An instance of this error class will be thrown when mongoose failed to cast a value.


Error.DivergentArrayError

Type:

An instance of this error will be thrown if you used an array projection and then modified the array in an unsafe way.


Error.DocumentNotFoundError

Type:

An instance of this error class will be thrown when save() fails because the underlying document was not found. The constructor takes one parameter, the conditions that mongoose passed to updateOne() when trying to update the document.


Error.MissingSchemaError

Type:

Thrown when you try to access a model that has not been registered yet


Error.MongooseBulkSaveIncompleteError

Type:

Thrown when some documents failed to save when calling bulkSave()


Error.MongooseServerSelectionError

Type:

Thrown when the MongoDB Node driver can't connect to a valid server to send an operation to.


Error.OverwriteModelError

Type:

Error.ParallelSaveError

Type:

An instance of this error class will be thrown when you call save() multiple times on the same document in parallel. See the FAQ for more information.


Error.StrictModeError

Type:

Thrown when your try to pass values to model constructor that were not specified in schema or change immutable properties whenstrict mode is "throw"


Error.StrictPopulateError

Type:

An instance of this error class will be returned when mongoose failed to populate with a path that is not existing.


Error.ValidationError

Type:

An instance of this error class will be thrown when validation failed. The errors property contains an object whose keys are the paths that failed and whose values are instances of CastError or ValidationError.


Error.ValidatorError

Type:

A ValidationError has a hash of errors that contain individualValidatorError instances.

Example:

const schema = Schema({ name: { type: String, required: true } });
const Model = mongoose.model('Test', schema);
const doc = new Model({});

// Top-level error is a ValidationError, **not** a ValidatorError
const err = doc.validateSync();
err instanceof mongoose.Error.ValidationError; // true

// A ValidationError `err` has 0 or more ValidatorErrors keyed by the
// path in the `err.errors` property.
err.errors['name'] instanceof mongoose.Error.ValidatorError;

err.errors['name'].kind; // 'required'
err.errors['name'].path; // 'name'
err.errors['name'].value; // undefined

Instances of ValidatorError have the following properties:


Error.VersionError

Type:

An instance of this error class will be thrown when you call save() after the document in the database was changed in a potentially unsafe way. See the versionKey option for more information.


Error.messages

Type:
See:

The default built-in validator error messages.


Error.prototype.name

Type:

The name of the error. The name uniquely identifies this Mongoose error. The possible values are: