{"record":{"id":"81fe8d2fa00f765d","repo":"hcengineering/platform","slug":"space-type-space-type-not-found","errorCode":null,"errorMessage":"Space type #${space.type} not found","messagePattern":"Space type #(.+?) not found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"plugins/training-resources/src/utils/createTrainingRequest.ts","lineNumber":48,"sourceCode":"      core.class.TypedSpace,\n      {\n        _id: parent.space\n      },\n      {\n        lookup: {\n          type: core.class.SpaceType\n        }\n      }\n    )\n\n    if (space === undefined) {\n      throw new Error(`Space #${parent.space} not found`)\n    }\n\n    const spaceType = space.$lookup?.type\n\n    if (spaceType === undefined) {\n      throw new Error(`Space type #${space.type} not found`)\n    }\n\n    const mixin = client.getHierarchy().as(space, spaceType.targetClass) as unknown as RolesAssignment\n    const personRefByAccountUuid = get(employeeRefByAccountUuidStore)\n    const employeeRefs = new Set(\n      roles\n        .map((roleId) => mixin[roleId] ?? [])\n        .flat()\n        .map((acc) => personRefByAccountUuid.get(acc))\n        .filter(notEmpty)\n    )\n\n    for (const employeeRef of employeeRefs) {\n      traineesMap.set(employeeRef, true)\n    }\n\n    attachedData.trainees = [...traineesMap.keys()]\n  }","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/plugins/training-resources/src/utils/createTrainingRequest.ts#L30-L66","documentation":"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.","triggerScenarios":"Space exists but its type field references a deleted SpaceType document, or the lookup config for type is missing so $lookup.type stays undefined.","commonSituations":"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.","solutions":["Verify the SpaceType document referenced by space.type exists.","Fix the space's type reference to point at a valid SpaceType.","Ensure the model declares the lookup so $lookup.type is populated (or fetch the type manually).","Re-run model migrations that (re)create default space types."],"exampleFix":"// before\nconst spaceType = space.$lookup?.type\n// after\nconst spaceType = space.$lookup?.type ?? await client.findOne(core.class.SpaceType, { _id: space.type })","handlingStrategy":"type-guard","validationCode":"const space = await client.findOne(core.class.Space, { _id: parent.space, lookup: { type: core.class.SpaceType } })\nif (space?.$lookup?.type === undefined) throw new Error('Space has no valid type')","typeGuard":"function hasSpaceType(s: Space): s is Space & { $lookup: { type: SpaceType } } { return s.$lookup?.type !== undefined }","tryCatchPattern":"try { return await createTrainingRequest(parent, roles) } catch (e) { if (e.message.includes('Space type')) repairSpaceType(parent.space); else throw e }","preventionTips":["Never delete SpaceType documents still referenced by spaces","Declare lookups in the model so $lookup.type populates","Re-run migrations that seed default space types"],"tags":["lookup","missing-object","space","configuration"],"backgroundTag":"referenced-object-not-found","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}