{"record":{"id":"34073f8651c03c40","repo":"hcengineering/platform","slug":"model-is-not-defined","errorCode":null,"errorMessage":"Model is not defined","messagePattern":"Model is not defined","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"foundations/core/packages/api-client/src/rest/adapter.ts","lineNumber":92,"sourceCode":"\n  async searchFulltext (query: SearchQuery, options: SearchOptions): Promise<SearchResult> {\n    return await this.client.searchFulltext(query, options)\n  }\n\n  async close (): Promise<void> {\n    // No ned to close the REST client\n  }\n\n  getHierarchy (): Hierarchy {\n    if (this.hierarchy === undefined) {\n      throw new Error('Hierarchy is not defined')\n    }\n    return this.hierarchy\n  }\n\n  getModel (): ModelDb {\n    if (this.model === undefined) {\n      throw new Error('Model is not defined')\n    }\n    return this.model\n  }\n}\n","sourceCodeStart":74,"sourceCodeEnd":97,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/foundations/core/packages/api-client/src/rest/adapter.ts#L74-L97","documentation":"Same lazy-initialization pattern as the hierarchy: getModel throws 'Model is not defined' when the adapter's ModelDb has not been assigned yet, i.e. the model was never loaded because initialization did not complete or was skipped.","triggerScenarios":"Calling client.getModel() on a RestClientAdapter before initialization assigns this.model; manually constructing the adapter and calling model-dependent methods immediately.","commonSituations":"Calling getModel in module top-level code that runs before async connect resolves, sharing an adapter across code paths where one path bypasses init, tests that build a partial adapter mock.","solutions":["Await complete adapter/client initialization before the first getModel call.","Obtain the client through connect() or the provided factory so model loading is guaranteed.","In tests, use the real init routine rather than a partially constructed adapter."],"exampleFix":"// before\nconst model = adapter.getModel() // adapter not initialized yet\n// after\nconst client = await connect(url, { workspace, token })\nconst model = client.getModel()","handlingStrategy":"try-catch","validationCode":"const client = await connect(url, { workspace, token }) // model is loaded during init\n// only then:\nconst model = client.getModel()","typeGuard":"function hasModel(c: unknown): c is { getModel: () => ModelDb } {\n  return typeof c === 'object' && c !== null && typeof (c as any).getModel === 'function'\n}","tryCatchPattern":"try {\n  const model = adapter.getModel()\n} catch (e) {\n  if (e instanceof Error && e.message === 'Model is not defined') {\n    throw new Error('Model accessed before the client finished initializing — await the connect/init promise first')\n  }\n  throw e\n}","preventionTips":["Gate all model-dependent code behind a single awaited init promise","Avoid exposing half-constructed adapters to other modules","Add an assertion/log on first use to catch premature access in development"],"tags":["initialization","lifecycle","typescript"],"backgroundTag":"client-not-initialized","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}