我正在尝试在我的模式之间建立一些关系,但是我的解决方案有一些问题。 这是我的设备模式:
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'}]
});
Mongoose抛出错误
类型错误: 未定义类型
ObjectID
在room
。您尝试过嵌套模式吗?您只能使用引用或数组进行嵌套。
如果我将room: {type: mongoose.Types.ObjectId, ref: 'Room'}
更改为room: {type: Number, ref: 'Room'}
,一切都正常工作。你能解释一下为什么会这样吗?
mongoose.Schema.Types.ObjectId
可以正常工作。奇怪的是,使用mongoose.Types.ObjectId
我能够创建用户对象和设备对象,并且在它们之间建立关联。当我添加第二个模型(房间)并在房间和设备之间建立关联时,出现了错误。 - niba