【发布时间】:2015-09-07 13:54:42
【问题描述】:
假设我在另一个模式(项目)中有一个嵌套模式(邀请),并且在尝试保存邀请对象时,我想检查项目模式中的父属性“启用”是否设置为 true 或 false在允许此人将邀请对象保存到邀请数组之前。显然,this.enabled 不起作用,因为它试图从不存在的invitationSchema 中获取它。如何让父模式上的“启用”属性进行验证?
有什么想法吗?感谢您的帮助!
var validateType = function(v) {
if (v === 'whateverCheck') {
return this.enabled || false; <== this doesn't work
} else {
return true;
}
};
var invitationSchema = new Schema({
name: { type: String },
type: {
type: String,
validate: [validateType, 'error message.']
}
});
var itemSchema = new Schema({
title: { type: String },
description: { type: String },
enabled: {type: Boolean}, <== trying to access this here
invitations: { type: [ invitationSchema ] },
});
var ItemModel = mongoose.model('Item', itemSchema, 'items');
var InvitationModel = mongoose.model('Invitation', invitationSchema);
【问题讨论】: