gpt4 book ai didi

javascript - 扩展模式 Mongoose 中的继承方法 "virtual"不起作用

转载 作者:行者123 更新时间:2023-12-03 12:12:47 26 4
gpt4 key购买 nike

我在模型 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 .所以我认为该方法 virtual来自 userSchema不是 teacherSchema 中的继承.如何设置 teacherSchema继承方法如 virtual来自 userSchema ?
型号
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
}

最佳答案

您正在使用 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、索引、方法和静态。
我正在描述 2 种方法,您可以使用这两种方法中的任何人,
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 }

关于javascript - 扩展模式 Mongoose 中的继承方法 "virtual"不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65452770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com