{"record":{"id":"dd7d50db7c291d30","repo":"Automattic/mongoose","slug":"the-2nd-parameter-to-mongoose-model-should-be","errorCode":null,"errorMessage":"The 2nd parameter to `mongoose.model()` should be a schema or a POJO","messagePattern":"The 2nd parameter to `mongoose\\.model\\(\\)` should be a schema or a POJO","errorType":"validation","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/connection.js","lineNumber":1463,"sourceCode":"    fn = name;\n    name = fn.name;\n  }\n\n  // collection name discovery\n  if (typeof schema === 'string') {\n    collection = schema;\n    schema = false;\n  }\n\n  if (utils.isObject(schema)) {\n    if (!schema.instanceOfSchema) {\n      schema = new Schema(schema);\n    } else if (!(schema instanceof this.base.Schema)) {\n      schema = schema._clone(this.base.Schema);\n    }\n  }\n  if (schema && !schema.instanceOfSchema) {\n    throw new MongooseError('The 2nd parameter to `mongoose.model()` should be a ' +\n      'schema or a POJO');\n  }\n\n  const defaultOptions = { cache: false, overwriteModels: this.base.options.overwriteModels };\n  const opts = Object.assign(defaultOptions, options, { connection: this });\n  if (this.models[name] && !collection && opts.overwriteModels !== true) {\n    // model exists but we are not subclassing with custom collection\n    if (schema?.instanceOfSchema && schema !== this.models[name].schema) {\n      throw new MongooseError.OverwriteModelError(name);\n    }\n    return this.models[name];\n  }\n\n  let model;\n\n  if (schema?.instanceOfSchema) {\n    applyPlugins(schema, this.plugins, null, '$connectionPluginsApplied');\n","sourceCodeStart":1445,"sourceCodeEnd":1481,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/connection.js#L1445-L1481","documentation":"When compiling a model, connection.model(name, schema) accepts a Schema instance, a plain object (auto-wrapped with new Schema(obj)), a string (reinterpreted as the collection name, leaving schema falsy), or a falsy value. Anything else that reaches the final check — a class/function (ES/TS classes are functions), an array, a number, true — fails schema.instanceOfSchema and throws (lib/connection.js:1466).","triggerScenarios":"conn.model('Test', SomeClass) passing a class where a schema is expected; conn.model('Test', ['name']); conn.model('Test', 42); a schema-shaped object from another library that is not a Mongoose Schema.","commonSituations":"TypeScript users passing a decorated or extended class as the schema; forgetting new Schema({...}); passing serialized JSON strings or config arrays as definitions.","solutions":["Wrap the definition: conn.model('Test', new Schema({ name: String }))","Or pass a POJO and let Mongoose convert: conn.model('Test', { name: String })","If you meant to pass a class, it belongs in the 1st parameter: conn.model(ClassName, schema)"],"exampleFix":"// before\nconn.model('User', UserSchemaClass);\n\n// after\nconn.model('User', new Schema({ name: String }));","handlingStrategy":"type-guard","validationCode":"function isValidModelSchema(v) {\n  return v == null || typeof v === 'string' || v?.instanceOfSchema === true ||\n    Object.prototype.toString.call(v) === '[object Object]';\n}\nif (!isValidModelSchema(schema)) {\n  throw new TypeError('2nd arg to model() must be a Schema, POJO, or collection-name string');\n}","typeGuard":"const isSchemaOrPojo = (v) =>\n  v?.instanceOfSchema === true ||\n  (typeof v === 'object' && v !== null && Object.prototype.toString.call(v) === '[object Object]');","tryCatchPattern":null,"preventionTips":["Always construct schemas explicitly: new Schema({...})","In TypeScript, type model parameters as (name: string, schema: Schema | Record<string, any>)","Remember a string 2nd argument is silently treated as a collection name — avoid relying on it accidentally"],"tags":["model","schema","invalid-argument-type","api-misuse"],"backgroundTag":"invalid-argument-type","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}