猫鼬无法读取未定义的属性“状态”

时间:2018-11-22 08:37:57

标签: javascript node.js mongoose ecmascript-6

我有使用Mongoose的NodeJS Express应用程序。我有一个房间架构设置:

export const RoomSchema = mongoose.Schema({
    ...
    status: {
        type: String,
        enum: ['HAVE', 'WANT', 'SWAP'],
        required: true
    },
    have: {
        type: NeedSchema,
        required: () => {
            return this.status == 'HAVE' || this.status == 'SWAP'
        }
    },
    want: {
        type: NeedSchema,
        required: () => {
            return this.status == 'WANT' || this.status == 'SWAP'
        }
    },
    ...

})

NeedSchema遵循的位置:

const NeedSchema = mongoose.Schema({
    building: {
        type: String,
        enum: ['AB', 'AC', 'AD']
    }
    room: {
        type: Number
    }
})

我希望仅当have Have SWAP status属性时才需要want属性。但是,当我运行一个create方法时,例如:

{
    "status": "HAVE"
}

我收到以下错误:

return _this.status == 'WANT' || _this.status == 'SWAP';
                   ^
TypeError: Cannot read property 'status' of undefined

在create方法中发送或不发送status都没关系,我会遇到相同的错误。

如果建筑物具有require属性,猫鼬会检查它吗?

0 个答案:

没有答案