hcengineering/platform · error

Training #${request.attachedTo} not found

Error message

Training #${request.attachedTo} not found

What it means

After the request is found, the provider reads request.$lookup?.attachedTo — the parent Training resolved via lookup. If the lookup did not populate (undefined), the parent training cannot be resolved for the title. This usually means the training document was deleted or the lookup configuration did not fetch it.

Source

Thrown at plugins/training-resources/src/functions/trainingRequestObjectTitleProvider.ts:43

  doc?: TrainingRequest
): Promise<string> {
  const request = await client.findOne<TrainingRequest>(
    training.class.TrainingRequest,
    {
      _id: ref
    },
    {
      lookup: {
        attachedTo: training.class.Training
      }
    }
  )
  if (request === undefined) {
    throw new Error(`Training request #${ref} not found`)
  }
  const trainingObject = request.$lookup?.attachedTo
  if (trainingObject === undefined) {
    throw new Error(`Training #${request.attachedTo} not found`)
  }

  const revisionLabel = client.getHierarchy().getAttribute(trainingObject._class, 'revision').label
  const revisionString = await translate(revisionLabel, {}, getCurrentLanguage())
  return `${trainingObject.code} • ${trainingObject.title} • ${revisionString} ${trainingObject.revision}`
}

View on GitHub (pinned to 63e28dc964)

Solutions

  1. Verify the parent Training (request.attachedTo) exists in the database.
  2. Check the model's lookup configuration so $lookup.attachedTo is populated by the query.
  3. Clean up orphaned TrainingRequest documents whose parent trainings are gone.
  4. Add a fallback title when the parent is missing instead of relying on lookup.

Example fix

// before
const trainingObject = request.$lookup?.attachedTo
// after
const trainingObject = request.$lookup?.attachedTo ?? await client.findOne(training.class.Training, { _id: request.attachedTo })
Defensive patterns

Strategy: validation

Validate before calling

const parent = await client.findOne(training.class.Training, { _id: request.attachedTo })
if (parent === undefined) return 'orphaned request'

Type guard

function hasAttachedParent<T extends { $lookup?: { attachedTo?: Doc } }>(r: T): boolean { return r.$lookup?.attachedTo !== undefined }

Try / catch

try { return await titleProvider(client, ref) } catch { return await fallbackTitle(client, ref) }

Prevention

When it happens

Trigger: TrainingRequest exists but its attachedTo Training no longer exists, or the findOne lookup options did not populate $lookup.attachedTo.

Common situations: Orphaned requests after a training hard-delete; hierarchy/lookup misconfiguration in the model; data restored partially from backup.

Related errors


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