Mongo Db error validator Error: Masekhta validation failed: _id: Error, expected `_id`

Viewed 1645

I'm trying to do simple scripts that add an object to Mongo and I get this error Error: Masekhta validation failed: _id: Error, expected _id I don't understand why I think that it's because of the unique validator but mongoose should handle the _id as I know.

First is Schema

const mongoose = require("mongoose");
const uniqueValidator = require("mongoose-unique-validator");

const Schema = mongoose.Schema;

const masekhtaSchema = new Schema({
  book: { type: String, required: true },
  pesukim: { type: String, required: false },
  selected: { type: Boolean, required: false },
});

masekhtaSchema.plugin(uniqueValidator);

module.exports = mongoose.model("Masekhta", masekhtaSchema);

Script:

const Masekhta = require("../models/masekhta");

const addMasekhta = async () => {
  const createdMasekhta = new Masekhta({
    book: "test",
    pesukim: "test",
    selected: false,
  });

  try {
    console.log("adding");
    await createdMasekhta.save();
  } catch (err) {
    console.log(err);
  }
};

addMasekhta();
Just to help a bit I have another one that looks pretty the same and it's working so the connection to the DB is clean.

Full Error if needed.

Error: Masekhta validation failed: _id: Error, expected `_id` to be unique. Value: `60c1dbdc13a2a7392c0ac7ce`
    at ValidationError.inspect (C:\Users\Admin\Desktop\All Projects\Coding\In Progress\Syium\backsiyum\node_modules\mongoose\lib\error\validation.js:47:26)
    at internal/per_context/primordials.js:23:32
    at formatValue (internal/util/inspect.js:774:19)
    at inspect (internal/util/inspect.js:336:10)
    at formatWithOptionsInternal (internal/util/inspect.js:2006:40)
    at formatWithOptions (internal/util/inspect.js:1888:10)
    at console.value (internal/console/constructor.js:313:14)
    at console.log (internal/console/constructor.js:348:61)
    at addMasekhta (C:\Users\Admin\Desktop\All Projects\Coding\In Progress\Syium\backsiyum\scripts\addMasekhta.js:14:13)
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  errors: {
    _id: ValidatorError: Error, expected `_id` to be unique. Value: `60c1dbdc13a2a7392c0ac7ce`
        at validate (C:\Users\Admin\Desktop\All Projects\Coding\In Progress\Syium\backsiyum\node_modules\mongoose\lib\schematype.js:1252:13)
        at C:\Users\Admin\Desktop\All Projects\Coding\In Progress\Syium\backsiyum\node_modules\mongoose\lib\schematype.js:1227:24
        at processTicksAndRejections (internal/process/task_queues.js:93:5) {
      properties: [Object],
      kind: 'unique',
      path: '_id',
      value: 60c1dbdc13a2a7392c0ac7ce,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    }
  },
  _message: 'Masekhta validation failed'
}

2 Answers

It seems that it comes from mongoose-unique-validator, the issue on github. This is an old package that hasn't been updated in 2 years, so I suggest you avoid it.

I had the same problem, my code worked using

await user.save({validatemodifyedOnly:true})

In Your case,

await createdMasektah.save({validatemodifyedOnly:true})

Hope this can help someone.

Related