hcengineering/platform · error · Error

product not found

Error message

product not found

What it means

Thrown in CreateVariant.svelte when creating an inventory Variant attached to a Product. Before calling client.addCollection, the code looks up the parent Product via client.findOne(inventory.class.Product, {_id: doc.attachedTo}) and throws if it cannot be found in the database. This guards against creating an orphaned variant pointing at a nonexistent or deleted product.

Source

Thrown at plugins/inventory-resources/src/components/CreateVariant.svelte:49

    _class: inventory.class.Variant,
    space: core.space.Workspace,
    _id: generateId(),
    collection: 'variants',
    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 productInstance = await client.findOne(inventory.class.Product, { _id: doc.attachedTo as Ref<Product> })
    if (productInstance === undefined) {
      throw new Error('product not found')
    }

    await client.addCollection(inventory.class.Variant, doc.space, doc.attachedTo, productInstance._class, 'variants', {
      name: doc.name,
      sku: doc.sku
    })
  }
</script>

<Card
  label={inventory.string.CreateVariant}
  okAction={create}
  canSave={doc.name.trim().length > 0 && doc.sku.trim().length > 0}
  on:close={() => {
    dispatch('close')
  }}
  on:changeContent
>

View on GitHub (pinned to 63e28dc964)

Solutions

  1. Refresh the product list and re-open the CreateVariant dialog against an existing product
  2. Verify doc.attachedTo actually holds a valid product _id before opening the dialog
  3. Re-login / reconnect the client so its data is synced, then retry
  4. Wrap create() in try/catch and show a user-friendly message instead of crashing

Example fix

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

Strategy: validation

Validate before calling

const product = await client.findOne(inventory.class.Product, { _id: doc.attachedTo as Ref<Product> })
if (doc.attachedTo === undefined || product === undefined) {
  throw new Error('Cannot create variant: attached product does not exist')
}

Type guard

function hasProduct (doc: { attachedTo?: Ref<Product> }): doc is { attachedTo: Ref<Product> } {
  return doc.attachedTo !== undefined
}

Try / catch

try {
  await create()
} catch (err) {
  if (err instanceof Error && err.message === 'product not found') {
    ui.notify('Selected product no longer exists; please reselect it.')
  } else throw err
}

Prevention

When it happens

Trigger: doc.attachedTo holds a Ref<Product> that does not resolve: the product was deleted before variant creation, the _id is stale/wrong, or the component was opened without a valid product selected.

Common situations: User deletes the product in another tab while the CreateVariant dialog is open; stale client cache after a sync issue; programmatic navigation passing an invalid product reference.

Related errors


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