繁体   English   中英

Mongoose模式引用和未定义类型'ObjectID'

[英]Mongoose schema reference and undefined type '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'}]
});

猫鼬抛出错误

TypeError:未定义类型room ObjectID您是否尝试嵌套Schemas? 您只能使用refs或数组进行嵌套。

如果我改变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'}]
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM