Node.js 获取用于Mongoose和MongoDB验证的父架构属性

Node.js 获取用于Mongoose和MongoDB验证的父架构属性,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,假设我在另一个架构(项目)中有一个嵌套的架构(邀请),并且在尝试保存邀请对象时,我希望在允许人员将邀请对象保存到邀请数组之前,检查项目架构中的父属性“enabled”是否设置为true或false。显然,这个.enabled不起作用,因为它试图使它脱离不存在的邀请模式。如何使父架构上的“enabled”属性得到验证 有什么想法吗?谢谢你的帮助 var validateType = function(v) { if (v === 'whateverCheck') { retu

假设我在另一个架构(项目)中有一个嵌套的架构(邀请),并且在尝试保存邀请对象时,我希望在允许人员将邀请对象保存到邀请数组之前,检查项目架构中的父属性“enabled”是否设置为true或false。显然,这个.enabled不起作用,因为它试图使它脱离不存在的邀请模式。如何使父架构上的“enabled”属性得到验证

有什么想法吗?谢谢你的帮助

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);
var validateType=函数(v){
如果(v=='whateverCheck'){

返回this.enabled | | false;通过调用
instance.parent();
,可以从嵌入式文档模型实例访问嵌入式文档的父级。因此,您可以从任何Mongoose中间件(如验证器或挂钩)执行此操作

在您的情况下,您可以:

var validateType = function(v) {
    if (v === 'whateverCheck') {
       return this.parent().enabled || false; // <== this does work
    } else {
        return true;
    }
};
var validateType=函数(v){
如果(v=='whateverCheck'){
返回此.parent().enabled | | false//