hcengineering/platform · error

Failed to find target project type: ${typeId}

Error message

Failed to find target project type: ${typeId}

What it means

createVacancy looks up the Recruit ProjectType document by the given typeId before creating a Vacancy attached to it. If no ProjectType with that _id exists in the workspace, the function throws instead of creating a vacancy referencing a nonexistent type. This guards referential integrity between vacancies and their project types.

Source

Thrown at plugins/bitrix/src/hr.ts:16

import { Organization } from '@hcengineering/contact'
import core, { PersonId, Client, Data, Doc, Ref, Status, TxOperations, generateId } from '@hcengineering/core'
import recruit, { Applicant, Vacancy } from '@hcengineering/recruit'
import task, { ProjectType } from '@hcengineering/task'

export async function createVacancy (
  rawClient: Client,
  name: string,
  typeId: Ref<ProjectType>,
  account: PersonId,
  company?: Ref<Organization>
): Promise<Ref<Vacancy>> {
  const client = new TxOperations(rawClient, account)
  const type = await client.findOne(task.class.ProjectType, { _id: typeId })
  if (type === undefined) {
    throw Error(`Failed to find target project type: ${typeId}`)
  }

  const sequence = await client.findOne(core.class.Sequence, { attachedTo: recruit.class.Vacancy })
  if (sequence === undefined) {
    throw new Error('sequence object not found')
  }

  const incResult = await client.update(sequence, { $inc: { sequence: 1 } }, true)

  const id: Ref<Vacancy> = generateId()
  await client.createDoc(
    recruit.class.Vacancy,
    core.space.Space,
    {
      name,
      description: type.shortDescription ?? '',
      fullDescription: null,
      private: false,

View on GitHub (pinned to 63e28dc964)

Solutions

  1. Verify the typeId exists: await client.findOne(task.class.ProjectType, { _id: typeId }) and log the result before calling createVacancy
  2. Fetch available project types for the workspace and pick the correct _id dynamically instead of hardcoding
  3. If migrating data, ensure ProjectType documents are created (e.g. via recruit plugin setup/seed) before creating vacancies
  4. Check you are connected to the correct workspace/account; ids are not globally unique across workspaces

Example fix

// before
await createVacancy(typeId, account, company) // typeId from old DB
// after
const type = await client.findOne(task.class.ProjectType, { _id: typeId })
if (type === undefined) throw new Error(`ProjectType ${typeId} missing; re-seed types`)
await createVacancy(type._id, account, company)
Defensive patterns

Strategy: validation

Validate before calling

const type = await client.findOne(task.class.ProjectType, { _id: typeId })
if (type === undefined) {
  throw new Error(`ProjectType ${typeId} not found in workspace; create/select a valid type first`)
}
await createVacancy(typeId, account, company)

Type guard

function isProjectType(t: ProjectType | undefined): t is ProjectType {
  return t !== undefined
}

Try / catch

try {
  await createVacancy(typeId, account, company)
} catch (err) {
  if (err instanceof Error && err.message.startsWith('Failed to find target project type')) {
    const types = await client.findAll(task.class.ProjectType, {})
    throw new Error(`typeId ${typeId} invalid; available: ${types.map(t => t._id).join(', ')}`)
  }
  throw err
}

Prevention

When it happens

Trigger: Calling createVacancy with a typeId that does not exist in task.class.ProjectType: a stale/deleted type id, an id from another workspace/account, a mistyped or truncated Ref, or calling before any project types were seeded.

Common situations: Migrating or restoring data where ProjectType documents were not copied; passing a hardcoded type id after it was deleted or re-created with a new id; running in a fresh workspace without the default Recruit project types created by setup; cross-workspace id reuse.

Related errors


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