{"record":{"id":"469e3369cffeff34","repo":"Automattic/mongoose","slug":"cannot-set-default-value-of-path-this-path-to","errorCode":null,"errorMessage":"Cannot set default value of path `${this.path}` to a mongoose Schema instance.","messagePattern":"Cannot set default value of path `(.+?)` to a mongoose Schema instance\\.","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/schemaType.js","lineNumber":478,"sourceCode":" *     m1.mixed.added = 1;\n *     console.log(m1.mixed); // { added: 1 }\n *     const m2 = new M;\n *     console.log(m2.mixed); // { added: 1 }\n *\n * @param {Function|any} val The default value to set\n * @return {any|undefined} Returns the set default value.\n * @api public\n */\n\nSchemaType.prototype.default = function(val) {\n  if (arguments.length === 1) {\n    if (val === void 0) {\n      this.defaultValue = void 0;\n      return void 0;\n    }\n\n    if (val?.instanceOfSchema) {\n      throw new MongooseError('Cannot set default value of path `' + this.path +\n        '` to a mongoose Schema instance.');\n    }\n\n    this.defaultValue = val;\n    return this.defaultValue;\n  } else if (arguments.length > 1) {\n    this.defaultValue = [...arguments];\n  }\n  return this.defaultValue;\n};\n\n/**\n * Declares the index options for this schematype.\n *\n * #### Example:\n *\n *     const s = new Schema({ name: { type: String, index: true })\n *     const s = new Schema({ name: { type: String, index: -1 })","sourceCodeStart":460,"sourceCodeEnd":496,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/schemaType.js#L460-L496","documentation":"SchemaType#default rejects mongoose Schema instances as default values (lib/schemaType.js:478): a Schema defines structure, not a value, so it is almost always a key mix-up where the schema was meant to be the field's type (a subdocument), not its default.","triggerScenarios":"`new Schema({ child: { type: String, default: childSchema } })`; `schema.path('child').default(childSchema)`; converting an object-literal field to a subdocument and leaving the schema in the wrong slot.","commonSituations":"Refactoring inline objects into named schemas; copy-paste from subdocument examples; tutorial code that mixes `type` and `default` keys.","solutions":["Define the path as a subdocument instead: `new Schema({ child: childSchema })` or `{ child: { type: childSchema } }`","If you want a default empty object, use a function: `default: () => ({})` (returning a fresh object per document)"],"exampleFix":"// before\nnew Schema({ child: { type: String, default: childSchema } }); // throws\n\n// after\nnew Schema({ child: { type: childSchema, default: () => ({}) } });","handlingStrategy":"validation","validationCode":"function assertValidDefault(path, def) {\n  if (def?.instanceOfSchema) {\n    throw new TypeError(`default for ${path} must be a value or a function, not a Schema; put the schema in 'type' instead`);\n  }\n}","typeGuard":"const isSchemaInstance = v => Boolean(v?.instanceOfSchema);","tryCatchPattern":null,"preventionTips":["Subdocument fields are declared as `child: childSchema`, never via default","Use factory functions (`default: () => ({})`) for object-shaped defaults so each doc gets a fresh copy"],"tags":["mongoose","schema","default","subdocument","options"],"backgroundTag":"invalid-default-value","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}