我有一个领域,并且在我所有的模型中都起着作用。
我在模型中的其他文件中创建了一个BaseSchema
:
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);
我在BaseSchema
中使用了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);
但是当我运行项目时,它向我显示此错误:
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)
什么是prboem?我该如何解决这个问题?
答案 0 :(得分:0)
我认为这可以简化,因为您基本上是在尝试扩展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();