{"record":{"id":"408f3428ffd1d83b","repo":"Automattic/mongoose","slug":"schema-hasn-t-been-registered-for-model-name","errorCode":null,"errorMessage":"Schema hasn't been registered for model \"${name}\".\nUse mongoose.model(name, schema)","messagePattern":"Schema hasn't been registered for model \"(.+?)\"\\.\nUse mongoose\\.model\\(name, schema\\)","errorType":"exception","errorClass":"MissingSchemaError","httpStatus":null,"severity":"error","filePath":"lib/connection.js","lineNumber":1509,"sourceCode":"    // Errors handled internally, so safe to ignore error\n    model.init().catch(function $modelInitNoop() {});\n\n    return model;\n  }\n\n  if (this.models[name] && collection) {\n    // subclassing current model with alternate collection\n    model = this.models[name];\n    schema = model.prototype.schema;\n    const sub = model.__subclass(this, schema, collection);\n    // do not cache the sub model\n    return sub;\n  }\n\n  if (arguments.length === 1) {\n    model = this.models[name];\n    if (!model) {\n      throw new MongooseError.MissingSchemaError(name);\n    }\n    return model;\n  }\n\n  if (!model) {\n    throw new MongooseError.MissingSchemaError(name);\n  }\n\n  if (this === model.prototype.db\n      && (!collection || collection === model.collection.name)) {\n    // model already uses this connection.\n\n    // only the first model with this name is cached to allow\n    // for one-offs with custom collection names etc.\n    if (!this.models[name]) {\n      this.models[name] = model;\n    }\n","sourceCodeStart":1491,"sourceCodeEnd":1527,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/connection.js#L1491-L1527","documentation":"conn.model(name) with a single argument is a pure lookup against models already compiled on THIS connection. If nothing is registered under that name, Mongoose throws MissingSchemaError ('Schema hasn't been registered for model ...') at lib/connection.js:1509 — it cannot invent a schema, so the model must have been registered first, or the name/connection is wrong.","triggerScenarios":"conn.model('User') before conn.model('User', userSchema) has run; a case typo ('user' vs 'User' — names are case-sensitive); a model registered on a different connection (mongoose.model on the default connection vs a createConnection instance) and looked up on this one.","commonSituations":"Multi-connection / multi-tenant apps where models are registered per connection; import-order bugs where a route executes before the model file; mixing mongoose.model() and conn.model() registries.","solutions":["Register first on the same connection: conn.model('User', userSchema), then look up","Check what exists: console.log(conn.modelNames()) before the lookup","Use the right registry: mongoose.model('User') for the default connection, conn.model('User') for a custom connection — they are separate","Fix casing/typos; model names are case-sensitive"],"exampleFix":"// before\nconst conn = mongoose.createConnection(uri);\nconst User = conn.model('User'); // MissingSchemaError: separate registry\n\n// after\nconst User = conn.model('User', userSchema); // register on this connection\n// or, if registered globally: mongoose.model('User')","handlingStrategy":"validation","validationCode":"if (!conn.modelNames().includes('User')) {\n  conn.model('User', userSchema); // register before lookup\n}\nconst User = conn.model('User');","typeGuard":null,"tryCatchPattern":"let User;\ntry {\n  User = conn.model('User');\n} catch (err) {\n  if (err instanceof mongoose.Error.MissingSchemaError) {\n    User = conn.model('User', userSchema); // first caller registers\n  } else {\n    throw err;\n  }\n}","preventionTips":["Remember registries are per connection: mongoose.model and conn.model do not share","Call conn.modelNames() when a lookup unexpectedly fails","Ensure model files are imported before routes that use them (import order at app bootstrap)","Model names are case-sensitive — match the registration exactly"],"tags":["model","missing-schema","model-lookup","multi-connection"],"backgroundTag":"model-not-registered","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}