hcengineering/platform · error · Error

category not found

Error message

category not found

What it means

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.

Source

Thrown at plugins/inventory-resources/src/components/CreateProduct.svelte:46

    _class: inventory.class.Product,
    space: core.space.Workspace,
    _id: generateId(),
    collection: 'products',
    modifiedOn: Date.now(),
    modifiedBy: '' as PersonId
  }

  const dispatch = createEventDispatcher()
  const client = getClient()

  export function canClose (): boolean {
    return doc.attachedTo.length === 0 && doc.name.length === 0
  }

  async function create () {
    const categoryInstance = await client.findOne(inventory.class.Category, { _id: doc.attachedTo as Ref<Category> })
    if (categoryInstance === undefined) {
      throw new Error('category not found')
    }

    await client.addCollection(
      inventory.class.Product,
      doc.space,
      doc.attachedTo,
      categoryInstance._class,
      'products',
      {
        name: doc.name
      }
    )
  }

  let categories: DropdownTextItem[] = []
  const categoriesQ = createQuery()
  $: categoriesQ.query(inventory.class.Category, {}, (result) => {
    categories = result.map((c) => {

View on GitHub (pinned to 63e28dc964)

Solutions

  1. Re-select the category in the UI so doc.attachedTo points to an existing document, then retry.
  2. Refresh the category list to purge cached ids of deleted categories.
  3. Add a pre-save existence check and a friendlier message instead of letting the raw error surface.

Example fix

// before
const categoryInstance = await client.findOne(inventory.class.Category, { _id: doc.attachedTo as Ref<Category> })
if (categoryInstance === undefined) throw new Error('category not found')
// after
const categoryInstance = await client.findOne(inventory.class.Category, { _id: doc.attachedTo as Ref<Category> })
if (categoryInstance === undefined) {
  ui.notify('The selected category no longer exists. Please choose another.')
  return
}
Defensive patterns

Strategy: validation

Validate before calling

const category = await client.findOne(inventory.class.Category, { _id: doc.attachedTo as Ref<Category> })
if (category === undefined) { ui.notify('Selected category no longer exists; pick another'); return }

Type guard

function categoryExists(c: Category | undefined): c is Category { return c !== undefined }

Try / catch

try {
  await create()
} catch (e) {
  if (e instanceof Error && e.message === 'category not found') {
    ui.notify('The category was deleted. Refresh and select a valid category.')
  } else throw e
}

Prevention

When it happens

Trigger: `doc.attachedTo` holds a stale/deleted category id, an id from a different workspace, or an unset/cast-invalid value when create() runs.

Common situations: 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.

Related errors


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