i have some field and it reapet in all of my models.
i create a BaseSchema
in in other file from my models:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
var util = require('util');
function BaseSchema() {
Schema.apply(this, arguments);
this.add({
owner: { type:String },
updateDate: { type: String },
updateBy: { type: String },
deleteDate: { type: String },
deleteby: { type: String },
createDate: { type: String },
createBy: { type: String },
});
}
util.inherits(BaseSchema, Schema);
and i use this BaseSchema
in RoleSchema
:
const mongoose = require("mongoose");
const schema = mongoose.Schema;
const BaseSchema = require("./baseEntity");
const RoleSchema = new BaseSchema.add({
name: { type: String, require: true },
description: { type: String },
scurityStamp: { type: String, require: true },
});
RoleSchema.pre('save',()=>{
scurityStamp='kianoush'
})
mongoose.model('Roles',RoleSchema);
but when i run the project it show me this error:
const RoleSchema = new BaseSchema.add({
^
TypeError: BaseSchema.add is not a constructor
at Object.<anonymous> (F:\Projects\Nodejs\SalesSignal\src\entity\role.js:5:20)
whats the prboem? how can i solve this problem???
I think this can be simplified as you're basically trying to extend your BaseSchema
:
import { model, Schema } from 'mongoose';
const BaseSchema = new Schema({
owner: { type:String },
updateDate: { type: String },
updateBy: { type: String },
deleteDate: { type: String },
deleteby: { type: String },
createDate: { type: String },
createBy: { type: String },
});
const RoleSchema = new Schema({
...BaseSchema.obj,
name: { type: String, required: true },
description: { type: String },
scurityStamp: { type: String, required: true },
});
// test
const RoleModel = mongoose.model('roles', RoleSchema);
const role = new RoleModel({ name: 'test', scurityStamp: 'test', owner: 'someOwner', createDate: new Date() });
await role.save();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.