猫鼬模式引用和未定义类型“ ObjectID”

如果:

我正在尝试在架构之间建立某种关系,而解决方案存在一些问题。这是我的设备架构:

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.ObjectIdObjectId构造函数,要在模式定义中使用的是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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章