I am struggling with creating simple scripts to add an object to MongoDB, and I keep encountering this error message:
Error: Masekhta validation failed: _id: Error, expected
_id
I can't figure out why this is happening. My guess is that it might be related to the unique validator, but I believe mongoose should handle the _id
properly.
Here is the 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();
If necessary, here is the full error:
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)
// Remaining stack trace omitted for brevity...
}