Node, Mongoose, problems saving multiple-depths of nested schema
You are extremely close, the problem is actually in your Schema
definitions. It all comes down to the difference between a Schema
object and a Model
object. When specifying mongoose Schema
with embedded documents, you can only point to other Schema
.
var DrawingToolsSchema = new Schema({ label: String, pens: [Pen] // uh-oh, broken! Pen is a Model.});
However, you do have this correct for your first level of embedded documents defined in StationeryBoxSchema
.
var StationeryBoxSchema = new Schema({ label: String, drawingTools: [DrawingToolsSchema], // yes! DrawingToolsSchema is a Schema measuringTools: [MeasuringToolsSchema] // this one too.});
This difference accounts for all of your unexpected behaviour later on.