hcengineering/platform · error

Space type # not found

Error message

Space type #${space.type} not found

What it means

After the space is found, the code reads space.$lookup?.type (the SpaceType descriptor) and throws when it is undefined. The space type is needed to apply the type mixin and determine role-based membership. This means the space's type reference is broken or the lookup did not populate.

Solutions

  1. Verify the SpaceType document referenced by space.type exists.
  2. Fix the space's type reference to point at a valid SpaceType.
  3. Ensure the model declares the lookup so $lookup.type is populated (or fetch the type manually).
  4. Re-run model migrations that (re)create default space types.

Example fix

// before
const spaceType = space.$lookup?.type
// after
const spaceType = space.$lookup?.type ?? await client.findOne(core.class.SpaceType, { _id: space.type })
Defensive patterns

Strategy: type-guard

Validate before calling

const space = await client.findOne(core.class.Space, { _id: parent.space, lookup: { type: core.class.SpaceType } })
if (space?.$lookup?.type === undefined) throw new Error('Space has no valid type')

Type guard

function hasSpaceType(s: Space): s is Space & { $lookup: { type: SpaceType } } { return s.$lookup?.type !== undefined }

Try / catch

try { return await createTrainingRequest(parent, roles) } catch (e) { if (e.message.includes('Space type')) repairSpaceType(parent.space); else throw e }

Prevention

When it happens

Trigger: Space exists but its type field references a deleted SpaceType document, or the lookup config for type is missing so $lookup.type stays undefined.

Common situations: Custom space types removed by a migration; partially restored backups; model misconfiguration where Space lookup type is not declared; spaces created by tooling without a type.

Related errors


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

Appendix: source

Thrown at plugins/training-resources/src/utils/createTrainingRequest.ts:48

      core.class.TypedSpace,
      {
        _id: parent.space
      },
      {
        lookup: {
          type: core.class.SpaceType
        }
      }
    )

    if (space === undefined) {
      throw new Error(`Space #${parent.space} not found`)
    }

    const spaceType = space.$lookup?.type

    if (spaceType === undefined) {
      throw new Error(`Space type #${space.type} not found`)
    }

    const mixin = client.getHierarchy().as(space, spaceType.targetClass) as unknown as RolesAssignment
    const personRefByAccountUuid = get(employeeRefByAccountUuidStore)
    const employeeRefs = new Set(
      roles
        .map((roleId) => mixin[roleId] ?? [])
        .flat()
        .map((acc) => personRefByAccountUuid.get(acc))
        .filter(notEmpty)
    )

    for (const employeeRef of employeeRefs) {
      traineesMap.set(employeeRef, true)
    }

    attachedData.trainees = [...traineesMap.keys()]
  }

View on GitHub (pinned to 63e28dc964)