hcengineering/platform · error · Error

Model is not defined

Error message

Model is not defined

What it means

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.

Source

Thrown at foundations/core/packages/api-client/src/rest/adapter.ts:92

  async searchFulltext (query: SearchQuery, options: SearchOptions): Promise<SearchResult> {
    return await this.client.searchFulltext(query, options)
  }

  async close (): Promise<void> {
    // No ned to close the REST client
  }

  getHierarchy (): Hierarchy {
    if (this.hierarchy === undefined) {
      throw new Error('Hierarchy is not defined')
    }
    return this.hierarchy
  }

  getModel (): ModelDb {
    if (this.model === undefined) {
      throw new Error('Model is not defined')
    }
    return this.model
  }
}

View on GitHub (pinned to 63e28dc964)

Solutions

  1. Await complete adapter/client initialization before the first getModel call.
  2. Obtain the client through connect() or the provided factory so model loading is guaranteed.
  3. In tests, use the real init routine rather than a partially constructed adapter.

Example fix

// before
const model = adapter.getModel() // adapter not initialized yet
// after
const client = await connect(url, { workspace, token })
const model = client.getModel()
Defensive patterns

Strategy: try-catch

Validate before calling

const client = await connect(url, { workspace, token }) // model is loaded during init
// only then:
const model = client.getModel()

Type guard

function hasModel(c: unknown): c is { getModel: () => ModelDb } {
  return typeof c === 'object' && c !== null && typeof (c as any).getModel === 'function'
}

Try / catch

try {
  const model = adapter.getModel()
} catch (e) {
  if (e instanceof Error && e.message === 'Model is not defined') {
    throw new Error('Model accessed before the client finished initializing — await the connect/init promise first')
  }
  throw e
}

Prevention

When it happens

Trigger: Calling client.getModel() on a RestClientAdapter before initialization assigns this.model; manually constructing the adapter and calling model-dependent methods immediately.

Common situations: 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.

Related errors


AI-assisted analysis of hcengineering/platform@63e28dc964 (2026-08-29). Data as JSON: /api/errors/34073f8651c03c40. Report an issue: GitHub.