【问题标题】: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
}

【问题讨论】:

    标签: javascript node.js mongodb mongoose mongoose-schema


    【解决方案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 }
    

    【讨论】:

      猜你喜欢
      • 2016-12-27
      • 1970-01-01
      • 2014-07-15
      • 2015-09-09
      • 2013-09-13
      • 2015-02-27
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      相关资源
      最近更新 更多