I have two schemas:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const _ = require("lodash");
const { regex } = require("../config/constants");
const { schemaErrors } = require("../config/errors");
const SiaSchema = require("./SiaModel");
const EmployeeSchema = new Schema(
{
first_name: {
type: String,
required: true,
trim: true,
},
last_name: {
type: String,
required: true,
trim: true,
},
mobile: {
type: String,
required: true,
validate: {
validator: function (v) {
return regex.uk.mobile.test(v);
},
message: schemaErrors.INVALID_MOBILE,
},
trim: true,
},
sia: [SiaSchema],
},
{ timestamps: { createdAt: "created_at", updatedAt: "updated_at" } }
);
EmployeeSchema.statics.addSia = async function (data) {
let options = { new: true, upsert: true, runValidators: true };
let sia = Object.assign({}, data);
delete sia._id;
let orQuery = [];
for (const attribute in sia) {
orQuery.push({ [`sia.${attribute}`]: sia[attribute] });
}
let existing = await this.findOne({ $or: orQuery });
if (existing) return Promise.resolve(existing);
return this.findByIdAndUpdate(data._id, { $push: { sia: sia } }, options);
};
module.exports = EmployeeSchema; // I am not exporting mongoose model here because model creation for all schemas is being handled by another module
and
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const _ = require("lodash");
const { regex } = require("../config/constants");
const { schemaErrors } = require("../config/errors");
const SiaSchema = new Schema(
{
sia_number: {
type: String,
required: true,
validate: {
validator: function (v) {
return regex.uk.sia.test(v);
},
message: schemaErrors.INVALID_SIA_NUMBER,
},
},
issue_date: {
type: Date,
required: true,
validate: {
validator: function (v) {
return v instanceof Date && v.getTime() < Date.now();
},
message: schemaErrors.INVALID_ISSUE_DATE,
},
},
expiration_date: {
type: Date,
required: true,
validate: {
validator: function (v) {
return v instanceof Date && v > this.issue_date;
},
message: schemaErrors.INVALID_EXPIRATION_DATE,
},
},
},
{ timestamps: { createdAt: "created_at", updatedAt: "updated_at" } }
);
module.exports = SiaSchema; // I am not exporting mongoose model here because model creation for all schemas is being handled by another module
I created an Employee. Then I called another API to add Sia for that Employee using the addSia static method defined in the Employee Schema, but it is giving me the following error:
"Validation failed: sia: Validation failed: _id: this.ownerDocument(...).model is not a function".
This is the body of the API and the data being passed as data
param to the addSia method:
{
"_id": "5f36cde0e4c4163838674219", // ID of the employee for whom I want to add sia
"sia_number": "0000000000000000",
"issue_date": "2020-08-01",
"expiration_date": "2025-08-01"
}
If I remove runValidators: true
from options
in the addSia method, the function works but the validations are not applied.
I would greatly appreciate any help with this.
Node.js version 11.9.0
Mongoose version 5.9.28
Mongodb version 4.2.3