我正在尝试在架构之间建立某种关系,而解决方案存在一些问题。这是我的设备架构:
var deviceSchema = schema({
name : String,
type : String,
room: {type: mongoose.Types.ObjectId, ref: 'Room'},
users: [{type:mongoose.Types.ObjectId, ref: 'User'}]
});
这里是房间模式:
var roomSchema = schema({
name : String,
image : String,
devices: [{type: mongoose.Types.ObjectId, ref: 'Device'}]
});
猫鼬抛出错误
类型错误:未定义的类型
ObjectID
,在room
你尝试筑巢的架构?您只能使用引用或数组进行嵌套。
如果我更改room: {type: mongoose.Types.ObjectId, ref: 'Room'},
为room: {type: Number, ref: 'Room'},
一切正常。您能解释一下为什么会这样吗?
mongoose.Types.ObjectId
是ObjectId
构造函数,要在模式定义中使用的是mongoose.Schema.Types.ObjectId
(或mongoose.Schema.ObjectId
)。
所以deviceSchema
应该看起来像这样:
var deviceSchema = schema({
name : String,
type : String,
room: {type: mongoose.Schema.Types.ObjectId, ref: 'Room'},
users: [{type:mongoose.Schema.Types.ObjectId, ref: 'User'}]
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句