{"record":{"id":"daa3ed6a41bce4b6","repo":"hcengineering/platform","slug":"category-not-found","errorCode":null,"errorMessage":"category not found","messagePattern":"category not found","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"plugins/inventory-resources/src/components/CreateProduct.svelte","lineNumber":46,"sourceCode":"    _class: inventory.class.Product,\n    space: core.space.Workspace,\n    _id: generateId(),\n    collection: 'products',\n    modifiedOn: Date.now(),\n    modifiedBy: '' as PersonId\n  }\n\n  const dispatch = createEventDispatcher()\n  const client = getClient()\n\n  export function canClose (): boolean {\n    return doc.attachedTo.length === 0 && doc.name.length === 0\n  }\n\n  async function create () {\n    const categoryInstance = await client.findOne(inventory.class.Category, { _id: doc.attachedTo as Ref<Category> })\n    if (categoryInstance === undefined) {\n      throw new Error('category not found')\n    }\n\n    await client.addCollection(\n      inventory.class.Product,\n      doc.space,\n      doc.attachedTo,\n      categoryInstance._class,\n      'products',\n      {\n        name: doc.name\n      }\n    )\n  }\n\n  let categories: DropdownTextItem[] = []\n  const categoriesQ = createQuery()\n  $: categoriesQ.query(inventory.class.Category, {}, (result) => {\n    categories = result.map((c) => {","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/plugins/inventory-resources/src/components/CreateProduct.svelte#L28-L64","documentation":"CreateProduct.svelte resolves the parent Category document by `_id` before adding the Product collection. If `client.findOne(inventory.class.Category, ...)` returns undefined, the attached category does not exist and 'category not found' is thrown, aborting product creation. It is a referential-integrity failure on the attachedTo link.","triggerScenarios":"`doc.attachedTo` holds a stale/deleted category id, an id from a different workspace, or an unset/cast-invalid value when create() runs.","commonSituations":"Category deleted while the create-product form was open, cross-space copy/paste of ids, cache showing a category that no longer exists in the DB.","solutions":["Re-select the category in the UI so doc.attachedTo points to an existing document, then retry.","Refresh the category list to purge cached ids of deleted categories.","Add a pre-save existence check and a friendlier message instead of letting the raw error surface."],"exampleFix":"// before\nconst categoryInstance = await client.findOne(inventory.class.Category, { _id: doc.attachedTo as Ref<Category> })\nif (categoryInstance === undefined) throw new Error('category not found')\n// after\nconst categoryInstance = await client.findOne(inventory.class.Category, { _id: doc.attachedTo as Ref<Category> })\nif (categoryInstance === undefined) {\n  ui.notify('The selected category no longer exists. Please choose another.')\n  return\n}","handlingStrategy":"validation","validationCode":"const category = await client.findOne(inventory.class.Category, { _id: doc.attachedTo as Ref<Category> })\nif (category === undefined) { ui.notify('Selected category no longer exists; pick another'); return }","typeGuard":"function categoryExists(c: Category | undefined): c is Category { return c !== undefined }","tryCatchPattern":"try {\n  await create()\n} catch (e) {\n  if (e instanceof Error && e.message === 'category not found') {\n    ui.notify('The category was deleted. Refresh and select a valid category.')\n  } else throw e\n}","preventionTips":["Refresh referenced category lists when documents are deleted elsewhere.","Clear the attachedTo field in the form if the selected category no longer exists.","Check referential integrity before enabling the create button."],"tags":["data-model","referential-integrity","missing-data"],"backgroundTag":"referenced-document-not-found","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}