{"record":{"id":"1ec063c24bfe7fa0","repo":"payloadcms/payload","slug":"the-collection-with-slug-string-collectionslug","errorCode":null,"errorMessage":"The collection with slug ${String(collectionSlug)} can't be found. Count Operation.","messagePattern":"The collection with slug (.+?) can't be found\\. Count Operation\\.","errorType":"http","errorClass":"APIError","httpStatus":500,"severity":"error","filePath":"packages/payload/src/collections/operations/local/count.ts","lineNumber":74,"sourceCode":"  where?: Where\n}\n\nexport async function countLocal<TSlug extends CollectionSlug>(\n  payload: Payload,\n  options: CountOptions<TSlug>,\n): Promise<{ totalDocs: number }> {\n  const {\n    collection: collectionSlug,\n    disableErrors,\n    overrideAccess = true,\n    trash = false,\n    where,\n  } = options\n\n  const collection = payload.collections[collectionSlug]\n\n  if (!collection) {\n    throw new APIError(\n      `The collection with slug ${String(collectionSlug)} can't be found. Count Operation.`,\n    )\n  }\n\n  return countOperation<TSlug>({\n    collection,\n    disableErrors,\n    overrideAccess,\n    req: await createLocalReq(options as CreateLocalReqOptions, payload),\n    trash,\n    where,\n  })\n}\n","sourceCodeStart":56,"sourceCodeEnd":88,"githubUrl":"https://github.com/payloadcms/payload/blob/00c58b35c0ed348ddc22daabf467b139727214fd/packages/payload/src/collections/operations/local/count.ts#L56-L88","documentation":"Thrown by the Local API `count` wrapper in packages/payload/src/collections/operations/local/count.ts:74 when the `collection` slug passed to `payload.count(...)` does not resolve to a registered collection in `payload.collections`. Because no HTTP status is supplied to APIError, it surfaces as HTTP 500 even though the cause is a bad client argument. It fires before `createLocalReq`, so no request context or access check runs.","triggerScenarios":"Calling `payload.count({ collection: 'post' })` where the registered slug is `'posts'` (singular vs plural), calling count with a dynamically-built slug string that does not exist, or invoking count before `payload.init()` has finished registering collections.","commonSituations":"Renaming a collection's `slug` in config but forgetting to update seed/migration scripts; importing a stale slug constant; casting an arbitrary string with `as CollectionSlug` to satisfy TypeScript while bypassing the literal-union check; running a script that uses a different Payload instance than the one whose config was loaded.","solutions":["Open the collection config file and copy the exact `slug:` value, then pass that string to `collection`.","Log `Object.keys(payload.collections)` right before the call to confirm the slug is registered at runtime.","Ensure `await payload.init()` has resolved before any `payload.count(...)` call (especially in standalone scripts, workers, or tests).","Remove any `as CollectionSlug` / `as any` casts on dynamic strings and let the literal-union type catch typos at compile time."],"exampleFix":"// before\nawait payload.count({ collection: 'post' as CollectionSlug }) // typo: real slug is 'posts'\n\n// after\nawait payload.count({ collection: 'posts' })","handlingStrategy":"validation","validationCode":"// Run before payload.count(...)\nfunction assertCollectionSlug(payload: Payload, slug: string): void {\n  if (!(slug in payload.collections)) {\n    throw new Error(\n      `Unknown collection slug '${slug}'. Registered: ${Object.keys(payload.collections).join(', ')}`,\n    )\n  }\n}\n\nassertCollectionSlug(payload, 'posts')\nawait payload.count({ collection: 'posts' })","typeGuard":"import type { Collection, CollectionSlug, Payload } from 'payload'\n\nconst slugIsRegistered = (\n  payload: Payload,\n  slug: string,\n): slug is CollectionSlug => slug in (payload.collections as Record<string, Collection>)\n\n// usage\nconst slug: string = getInputSlug()\nif (slugIsRegistered(payload, slug)) {\n  await payload.count({ collection: slug }) // slug narrowed to CollectionSlug\n} else {\n  // handle unknown slug\n}","tryCatchPattern":"try {\n  await payload.count({ collection: slug })\n} catch (err) {\n  if (err instanceof APIError && /can't be found\\. Count Operation/.test(err.message)) {\n    // slug not registered — fix the caller, do not retry blindly\n  } else {\n    throw err\n  }\n}","preventionTips":["Always type the slug as CollectionSlug (never cast dynamic strings with `as`).","In standalone scripts, await payload.init() before any Local API call.","Centralize slug constants in one module imported by config and callers alike."],"tags":["local-api","collection","slug","configuration"],"backgroundTag":null,"analyzedSha":"00c58b35c0ed348ddc22daabf467b139727214fd","analyzedAt":"2026-08-12T20:45:03.758Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}