我有使用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
属性,猫鼬会检查它吗?