通过猫鼬模式中的函数返回猫鼬类型

时间:2020-09-08 19:52:04

标签: mongodb validation mongoose

new mongoose.Schema({
status:{
type:String,
enum:['General','Private']
}
user: {
type:function() {return this.status == 'Private' ? mongoose.Schema.Types.ObjectId : [] }
}
})

关于上述架构,是否有一种方法可以使用基于另一个属性的函数来返回属性的类型?我可以使用“类型”以外的其他属性来执行此操作。在用户属性中,我在'type'中有一个函数,希望返回该类型,但在以下情况下仍会收到错误:

[0]     throw new TypeError(`Invalid schema configuration: \`${name}\` is not ` +
[0]     ^
[0]
[0] TypeError: Invalid schema configuration: `Type` is not a valid type at path `user`. See xxxxxxx for a list of valid schema types.

0 个答案:

没有答案