{"record":{"id":"c8fe9e7a5e3d9391","repo":"Automattic/mongoose","slug":"parameter-doc-to-init-must-be-an-object-got","errorCode":null,"errorMessage":"Parameter \"doc\" to init() must be an object, got \"${doc}\" (type ${typeof doc})","messagePattern":"Parameter \"doc\" to init\\(\\) must be an object, got \"(.+?)\" \\(type (.+?)\\)","errorType":"exception","errorClass":"ObjectParameterError","httpStatus":null,"severity":"error","filePath":"lib/document.js","lineNumber":663,"sourceCode":" * Note that `init` hooks are [synchronous](https://mongoosejs.com/docs/middleware.html#synchronous).\n *\n * @param {object} doc raw document returned by mongo\n * @param {object} [opts]\n * @param {boolean} [opts.hydratedPopulatedDocs=false] If true, hydrate and mark as populated any paths that are populated in the raw document\n * @param {Function} [fn]\n * @api public\n * @memberOf Document\n * @instance\n */\n\nDocument.prototype.init = function(doc, opts, fn) {\n  if (typeof opts === 'function') {\n    fn = opts;\n    opts = null;\n  }\n\n  if (doc == null) {\n    throw new ObjectParameterError(doc, 'doc', 'init');\n  }\n\n  this.$__init(doc, opts);\n\n  if (fn) {\n    fn(null, this);\n  }\n\n  return this;\n};\n\n/**\n * Alias for [`.init`](https://mongoosejs.com/docs/api/document.html#Document.prototype.init())\n *\n * @api public\n */\n\nDocument.prototype.$init = function() {","sourceCodeStart":645,"sourceCodeEnd":681,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/document.js#L645-L681","documentation":"Document#init() hydrates a document from a raw MongoDB doc and requires that doc to be present. Passing null or undefined throws ObjectParameterError immediately; init() also accepts an optional callback, but a missing doc is always fatal.","triggerScenarios":"model.init(null), doc.init(undefined), or doc.init(cached) where cached is null after a cache miss; hydrating with a findOne() result without checking it first.","commonSituations":"Manual hydration from caches or message queues where entries can be missing; custom loaders or hooks that call init() on possibly-absent records.","solutions":["Null-check before hydrating: if (raw) model.init(raw)","Handle the 'not found' case explicitly instead of forwarding null into init()"],"exampleFix":"// before\nmodel.init(cache.get(id)); // null on cache miss → throws\n\n// after\nconst raw = cache.get(id);\nif (raw != null) model.init(raw);","handlingStrategy":"validation","validationCode":"function hydrate(model, raw) {\n  if (raw == null) throw new Error('cannot init: raw doc is missing');\n  return model.init(raw);\n}","typeGuard":"const hasRawDoc = (v) => v != null && typeof v === 'object';","tryCatchPattern":"try {\n  model.init(raw);\n} catch (err) {\n  if (err.name === 'ObjectParameterError') {\n    // source produced null (cache miss / not found) — handle explicitly\n    return loadFromDatabase();\n  }\n  throw err;\n}","preventionTips":["Check for null before hydrating from caches, queues, or query results","Treat a missing raw doc as a distinct code path (miss/not-found), never an init() argument","Wrap hydration of external data with validation that rejects null early"],"tags":["document","init","hydration","objectparametererror","mongoose"],"backgroundTag":"invalid-argument-type","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}