{"record":{"id":"fd11f82bb1686e1a","repo":"hcengineering/platform","slug":"cannot-resolve-a-document-for-meta-meta","errorCode":null,"errorMessage":"Cannot resolve a document for meta ${meta}","messagePattern":"Cannot resolve a document for meta (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"plugins/controlled-documents-resources/src/utils.ts","lineNumber":181,"sourceCode":"\n  let targetDocument: ControlledDocument | undefined\n  for (const doc of docs) {\n    if (doc.state === DocumentState.Effective) {\n      targetDocument = doc\n      break\n    } else if (doc.state === DocumentState.Deleted && targetDocument === undefined) {\n      targetDocument = doc\n    } else if (doc.state === DocumentState.Obsolete && targetDocument === undefined) {\n      targetDocument = doc\n    } else if (doc.state === DocumentState.Draft) {\n      targetDocument = doc\n    } else if (doc.state === DocumentState.Archived) {\n      break\n    }\n  }\n\n  if (targetDocument === undefined) {\n    throw new Error('Cannot resolve a document for meta ' + meta)\n  }\n\n  const projectDoc = await client.findOne(\n    documents.class.ProjectDocument,\n    { document: targetDocument._id },\n    {\n      lookup: {\n        project: documents.class.Project\n      },\n      sort: {\n        '$lookup.project.createdOn': SortingOrder.Descending\n      },\n      limit: 1\n    }\n  )\n\n  const project = projectDoc?.project ?? documents.ids.NoProject\n","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/plugins/controlled-documents-resources/src/utils.ts#L163-L199","documentation":"getDocumentMetaLinkFragment walks the revision history of a controlled document's meta to find a target document, and throws if none can be resolved. This means the meta document exists but no associated ProjectDocument in a resolvable state (not archived/terminated) could be found. It guards link generation from producing broken locations.","triggerScenarios":"Generating a link fragment for a ControlledDocumentMeta whose document chain only contains Archived documents or has no documents at all; orphaned meta records after deletions.","commonSituations":"Linking to a controlled document whose project document was archived; stale meta records left after data cleanup; importing meta data without corresponding ProjectDocuments.","solutions":["Ensure a non-archived ProjectDocument exists and is linked to the meta's document chain","Restore/re-create the ProjectDocument if it was wrongly archived or deleted","Skip link generation for archived/terminated documents instead of resolving a fragment","Clean up orphaned meta records so links are only generated for valid documents"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"const target = await walkDocumentChain(client, meta) // equivalent traversal\nif (target === undefined) return undefined // skip link generation","typeGuard":null,"tryCatchPattern":"try {\n  return await getDocumentMetaLinkFragment(meta)\n} catch (err) {\n  if (err.message.startsWith('Cannot resolve a document for meta')) {\n    return undefined // render no link for archived/orphaned meta\n  }\n  throw err\n}","preventionTips":["Skip link generation for archived/terminated documents","Clean up orphaned meta records during deletions","Verify the document chain before generating link fragments","Keep ProjectDocument links intact during migrations"],"tags":["controlled-documents","link-resolution","data-integrity"],"backgroundTag":"document-not-resolvable","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}