扩展模式猫鼬中的“虚拟”继承方法不起作用

Posted

技术标签:

【中文标题】扩展模式猫鼬中的“虚拟”继承方法不起作用【英文标题】:Inheritance method "virtual" in extend schema mongoose doesn't work 【发布时间】:2021-04-03 18:12:15 【问题描述】:

我在模型UserSchema中有以下方法:

userSchema.virtual('password')
    .set(function(password) 
            this._password = password;
            this.salt = this.makeSalt();
            this.hashed_password = this.encryptPassword(password);
    )
    .get(function() 
            return this._password;
    
);

但是当我创建新的teacher 时出现错误hashed_password is required。所以我认为userSchema 中的virtual 方法不是teacherSchema 中的继承。如何从userSchema 设置teacherSchema 继承方法,如virtual

型号

const mongoose = require('mongoose');
extend = require('mongoose-extend-schema');
const Schema = mongoose.Schema;

const userSchema = new Schema(
    name: 
        type: String,
        trim: true,
        required: true,
        maxLength: 32
    ,
    surname: 
        type: String,
        trim: true,
        required: true,
        maxLength: 32
    ,
    email: 
        type: String,
        unique: true,
        trim: true,
        required: true,
        lowercase: true
    ,
hashed_password: 
        type: String,
        required: true
    ,
    initials: String
);

userSchema.virtual('password')
    .set(function(password) 
            this._password = password;
            this.salt = this.makeSalt();
            this.hashed_password = this.encryptPassword(password);
    )
    .get(function() 
            return this._password;
    
);



const studentSchema = extend(userSchema, 
    teachers: []   
);

const teacherSchema = extend(userSchema, 
    isActiveTutor: 
        type: Boolean,
        default: false
    
)
  


const User =  mongoose.model('User', userSchema);
const Student = mongoose.model('Student', studentSchema);
const Teacher = mongoose.model('Teacher', teacherSchema);

module.exports = 
    User,
    Student,
    Teacher

【问题讨论】:

【参考方案1】:

您正在使用mongoose-extend-schema NPM,这只会将对象从源架构合并到新架构,像这样,

function extendSchema (Schema, definition, options) 
  return new mongoose.Schema(
    Object.assign(, Schema.obj, definition),
    options
  );
 

mongoose 中有一个名为clone() 的新方法,它返回模式的深层副本,以及一个add(),将键路径/模式类型对添加到此模式,您还可以add() 另一个模式和复制所有路径、虚拟变量、getter、setter、索引、方法和静态变量。

我描述了两种方法,你可以使用这两种方法中的任何人,

1) 克隆父架构并向其添加新属性

// Skipping your 1) User Schema and 2) Virtual Function, because its remain same

const studentSchema = userSchema.clone();
studentSchema.add(
    teachers: []
);

const teacherSchema = userSchema.clone();
teacherSchema.add(
    isActiveTutor: 
        type: Boolean,
        default: false
    
);

const User = mongoose.model('User', userSchema);
const Student = mongoose.model('Student', studentSchema);
const Teacher = mongoose.model('Teacher', teacherSchema);

module.exports =  User, Student, Teacher 

2) 创建新架构并向其添加父架构

// Skipping your 1) User Schema and 2) Virtual Function, because its remain same

const studentSchema = new Schema(
    teachers: []
).add(userSchema);

const teacherSchema = new Schema(
    isActiveTutor: 
        type: Boolean,
        default: false
    
).add(userSchema);

const User = mongoose.model('User', userSchema);
const Student = mongoose.model('Student', studentSchema);
const Teacher = mongoose.model('Teacher', teacherSchema);

module.exports =  User, Student, Teacher 

【讨论】:

以上是关于扩展模式猫鼬中的“虚拟”继承方法不起作用的主要内容,如果未能解决你的问题,请参考以下文章

猫鼬中的保存功能不起作用

护照本地猫鼬中的密码验证器选项不起作用

使用数组过滤器更新多个嵌套数组在猫鼬中不起作用

在猫鼬中排序

如何删除猫鼬中的Many

猫鼬混合模式不起作用