【发布时间】:2018-06-30 08:04:50
【问题描述】:
我有 4 个模型,Grid、Container、Row 和 Column。我需要初始化一个Grid,并将其余 3 个模型嵌套在其中。
我遇到的问题是创建了对Container、Row 和Column 的引用(ObjectIds),但文档本身没有保存。仅保存Grid 文档。
我希望它将其余模型也保存到各自的集合中......
我错过了什么?
这是我的模型/模式:
const GridSchema = new Schema({
container: {type: mongoose.Schema.Types.ObjectId, ref: 'Container', required: true}
});
const ContainerSchema = new Schema({
rows: [{type: mongoose.Schema.Types.ObjectId, ref: 'Row'}]
});
const RowSchema = new Schema({
columns: [{type: mongoose.Schema.Types.ObjectId, ref: 'Column'}]
});
const ColumnSchema = new Schema({
modules: [{type: mongoose.Schema.Types.ObjectId, ref: 'Module'}]
});
const Grid = mongoose.model('Grid', GridSchema);
const Container = mongoose.model('Container', ContainerSchema);
const Row = mongoose.model('Row', RowSchema);
const Column = mongoose.model('Column', ColumnSchema);
这是我如何初始化网格并保存它:
const grid = new Grid({
container: new Container({
rows: [
new Row({
column: [
new Column({
modules: []
})
]
})
]
})
});
grid.save((err, grid) => {
console.log(err, grid); // No error is thrown
});
【问题讨论】: