Wednesday, 30 December 2020

Inheritance method "virtual" in extend schema mongoose doesn't work

I have below method in model 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;
    }
);

but when I create new teacher I have error hashed_password is required. So I think that method virtual from userSchema isn't inheritance in teacherSchema. How can I set that teacherSchema inheritances method like virtual from userSchema?

model

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
}


from Inheritance method "virtual" in extend schema mongoose doesn't work

No comments:

Post a Comment