{"record":{"id":"4b0f474aa192b543","repo":"hcengineering/platform","slug":"parent-not-found","errorCode":null,"errorMessage":"parent not found","messagePattern":"parent not found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"plugins/drive/src/utils.ts","lineNumber":33,"sourceCode":"\nimport { type AttachedData, type Data, type Ref, type TxOperations, generateId } from '@hcengineering/core'\nimport { type Location } from '@hcengineering/ui'\n\nimport drive, { driveId } from './plugin'\nimport type { Drive, File, FileVersion, Folder } from './types'\n\n/** @public */\nexport async function createFolder (\n  client: TxOperations,\n  space: Ref<Drive>,\n  data: Omit<Data<Folder>, 'path'>\n): Promise<Ref<Folder>> {\n  let path: Array<Ref<Folder>> = []\n\n  if (data.parent !== drive.ids.Root) {\n    const parent = await client.findOne(drive.class.Folder, { _id: data.parent })\n    if (parent === undefined) {\n      throw new Error('parent not found')\n    }\n    path = [parent._id, ...parent.path]\n  }\n\n  return await client.createDoc(drive.class.Folder, space, { ...data, path })\n}\n\n/** @public */\nexport async function createFile (\n  client: TxOperations,\n  space: Ref<Drive>,\n  parent: Ref<Folder>,\n  data: Omit<AttachedData<FileVersion>, 'version'>\n): Promise<Ref<File>> {\n  const folder = await client.findOne(drive.class.Folder, { _id: parent })\n  const path = folder !== undefined ? [folder._id, ...folder.path] : []\n\n  const version = 1","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/plugins/drive/src/utils.ts#L15-L51","documentation":"createFolder resolves the requested parent Folder to build the new folder's path; if the parent is not Root and no Folder with data.parent exists, it throws 'parent not found'. This keeps the folder path chain consistent and prevents orphaned folders.","triggerScenarios":"Calling createFolder with data.parent referencing a deleted or non-existent Folder; race where the parent is deleted between UI selection and creation; invalid parent id from client-side state.","commonSituations":"Two users concurrently: one deletes parent folder, another creates a subfolder in it; stale UI tree after deletions; import scripts with broken parent references.","solutions":["Verify the parent folder exists (client.findOne on drive.class.Folder) before calling createFolder","Re-select a valid parent in the UI after a deletion, then retry","Handle deletions that should cascade or block subfolder creation","Catch the error and refresh the folder tree before retrying"],"exampleFix":"// before\nawait createFolder(client, space, { parent: staleParentId, name })\n// after\nconst parent = await client.findOne(drive.class.Folder, { _id: parentId })\nif (parent === undefined) throw new Error('Please refresh: parent folder no longer exists')\nawait createFolder(client, space, { parent: parentId, name })","handlingStrategy":"validation","validationCode":"if (data.parent !== drive.ids.Root) {\n  const parent = await client.findOne(drive.class.Folder, { _id: data.parent })\n  if (parent === undefined) {\n    throw new Error('Refresh the folder tree: parent no longer exists')\n  }\n}","typeGuard":"async function parentExists(client: Client, parent: Ref<Folder>): Promise<boolean> {\n  return parent === drive.ids.Root ||\n    (await client.findOne(drive.class.Folder, { _id: parent })) !== undefined\n}","tryCatchPattern":"try {\n  await createFolder(client, space, data)\n} catch (err) {\n  if (err.message === 'parent not found') {\n    await refreshFolderTree()\n    // re-pick a valid parent, then retry\n    return\n  }\n  throw err\n}","preventionTips":["Refresh the folder tree after any deletion","Re-validate the selected parent id at creation time","Block deletion of folders that have pending create operations","Handle concurrent-delete races with a re-check before create"],"tags":["drive","folder","missing-parent"],"backgroundTag":"parent-not-found","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}