{"record":{"id":"bfbf951b577c2125","repo":"Automattic/mongoose","slug":"cannot-create-documentarrayelement-schematype-with","errorCode":null,"errorMessage":"Cannot create DocumentArrayElement schematype without a parent","messagePattern":"Cannot create DocumentArrayElement schematype without a parent","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/schema/documentArrayElement.js","lineNumber":30,"sourceCode":"/**\n * DocumentArrayElement SchemaType constructor. Mongoose calls this internally when you define a new document array in your schema.\n *\n * #### Example:\n *     const schema = new Schema({ users: [{ name: String }] });\n *     schema.path('users.$'); // SchemaDocumentArrayElement with schema `new Schema({ name: String })`\n *\n * @param {string} path\n * @param {Schema} schema\n * @param {object} options\n * @param {Schema} parentSchema\n * @inherits SchemaType\n * @api public\n */\n\nfunction SchemaDocumentArrayElement(path, schema, options, parentSchema) {\n  this.$parentSchemaType = options?.$parentSchemaType;\n  if (!this.$parentSchemaType) {\n    throw new MongooseError('Cannot create DocumentArrayElement schematype without a parent');\n  }\n  delete options.$parentSchemaType;\n\n  SchemaType.call(this, path, options, 'DocumentArrayElement', parentSchema);\n\n  this.$isMongooseDocumentArrayElement = true;\n  this.Constructor = options?.Constructor;\n  this.schema = schema;\n}\n\n/**\n * This schema type's name, to defend against minifiers that mangle\n * function names.\n *\n * @api public\n */\nSchemaDocumentArrayElement.schemaName = 'DocumentArrayElement';\n","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/schema/documentArrayElement.js#L12-L48","documentation":"Internal invariant error: SchemaDocumentArrayElement (the schematype Mongoose auto-creates for paths inside an array element, e.g. `arr.name`) requires `options.$parentSchemaType` — a back-reference to the owning SchemaDocumentArray — and its constructor throws immediately when it is missing. You should never see this from normal model code; it means some code path is constructing or copying element schematypes without the internal wiring Mongoose sets up itself.","triggerScenarios":"Directly doing `new mongoose.Schema.Types.DocumentArrayElement(path, subSchema, opts)` without `$parentSchemaType`; plugins or utilities that traverse `schema.paths` and rebuild/re-register paths (deep-clone of schemas, schema merging libraries) and drop the `$parentSchemaType` option; copying a DocumentArrayElement path into another schema via `add()`.","commonSituations":"Schema-merging/normalizing plugins (e.g. mongoose-merge-plugin style) written against older Mongoose internals after a Mongoose upgrade; utility code that deep-clones schema definitions; mixing Mongoose major versions with stale compiled plugins.","solutions":["Never construct DocumentArrayElement yourself — declare `arr: [subSchema]` and let Mongoose build element types","Update or replace plugins that manipulate `schema.paths`/`schema.nested` internals; check their Mongoose version support","If you genuinely must build one, pass the owning array schematype: `opts.$parentSchemaType = mySchemaDocumentArrayInstance`","Pin/upgrade Mongoose and plugins together and re-run your test suite — this error is a compatibility smell"],"exampleFix":"// before\nconst el = new mongoose.Schema.Types.DocumentArrayElement('name', sub, {}); // throws\n\n// after\nconst parent = new Schema({ items: [sub] }); // let Mongoose create element paths\n// access via parent.path('items').schema.path('name') if needed","handlingStrategy":"validation","validationCode":"// guard your schema-manipulation utilities:\nfunction assertNoRawElementTypes(schema) {\n  for (const [path, type] of Object.entries(schema.paths)) {\n    if (type.$isMongooseDocumentArrayElement && !type.$parentSchemaType) {\n      throw new Error(`plugin must not re-register ${path} without $parentSchemaType`);\n    }\n  }\n}","typeGuard":null,"tryCatchPattern":"try { buildSchemaViaPlugin(parts); } catch (err) { if (/DocumentArrayElement schematype without a parent/.test(err.message)) { throw new Error('schema plugin incompatible with this mongoose version — update it'); } throw err; }","preventionTips":["Never construct Schema.Types.DocumentArrayElement directly","Upgrade schema-manipulation plugins together with mongoose majors","Prefer declarative schema definitions over path-level surgery"],"tags":["mongoose","internal","schematype","plugin","compatibility"],"backgroundTag":"invalid-schema-option","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}