payloadcms/payload · error · APIError

The collection with slug ${String(collectionSlug)} can't be

Error message

The collection with slug ${String(collectionSlug)} can't be found. Find By ID Operation.

What it means

Thrown by the Local API `findByID` wrapper in packages/payload/src/collections/operations/local/findByID.ts:152 when `payload.findByID({ collection, id })` targets a slug not in `payload.collections`. Defaults to HTTP 500. Fires before the document lookup, so an invalid slug never reaches the database.

Source

Thrown at packages/payload/src/collections/operations/local/findByID.ts:152

    currentDepth,
    data,
    depth,
    disableErrors = false,
    draft = false,
    flattenLocales,
    includeLockStatus,
    joins,
    overrideAccess = true,
    populate,
    select,
    showHiddenFields,
    trash = false,
  } = options

  const collection = payload.collections[collectionSlug]

  if (!collection) {
    throw new APIError(
      `The collection with slug ${String(collectionSlug)} can't be found. Find By ID Operation.`,
    )
  }

  return findByIDOperation<TSlug, TDisableErrors, TSelect>({
    id,
    collection,
    currentDepth,
    data,
    depth,
    disableErrors,
    draft,
    flattenLocales,
    includeLockStatus,
    joins,
    overrideAccess,
    populate,
    req: await createLocalReq(options as CreateLocalReqOptions, payload),

View on GitHub (pinned to 00c58b35c0)

Solutions

  1. Verify the slug against the collection config and `Object.keys(payload.collections)`.
  2. If the slug comes from a request parameter, allowlist it against registered slugs.
  3. Ensure `payload.init()` resolved.
  4. Remove `as CollectionSlug` casts on untrusted values.

Example fix

// before
await payload.findByID({ collection: 'category', id }) // slug is 'categories'

// after
await payload.findByID({ collection: 'categories', id })
Defensive patterns

Strategy: validation

Validate before calling

function assertCollectionSlug(payload: Payload, slug: string): void {
  if (!(slug in payload.collections)) {
    throw new Error(`Unknown collection slug '${slug}'`)
  }
}
assertCollectionSlug(payload, 'categories')
await payload.findByID({ collection: 'categories', id })

Type guard

const slugIsRegistered = (payload: Payload, slug: string): slug is CollectionSlug =>
  slug in (payload.collections as Record<string, unknown>)

if (slugIsRegistered(payload, slug)) {
  await payload.findByID({ collection: slug, id })
}

Try / catch

try {
  await payload.findByID({ collection: slug, id })
} catch (err) {
  if (err instanceof APIError && /Find By ID Operation/.test(err.message)) {
    // unknown slug — correct it
  } else throw err
}

Prevention

When it happens

Trigger: Calling `payload.findByID({ collection: 'media-item', id })` when the slug is `'media'`; fetching a related document by slug received from a relationship field whose target collection was removed; route handler using a path param as the slug without validation.

Common situations: Slug typo; collection removed but relationship fields still reference it; `as CollectionSlug` cast on request input; calling before `payload.init()`.

Related errors


AI-assisted analysis of payloadcms/payload@00c58b35c0 (2026-08-12). Data as JSON: /api/errors/c8c0fa6283928b78. Report an issue: GitHub.