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. Count Versions Operation. What it means
Thrown by the Local API `countVersions` wrapper in packages/payload/src/collections/operations/local/countVersions.ts:59 when the `collection` slug supplied to `payload.countVersions(...)` is not present in `payload.collections`. It defaults to HTTP 500 since no status is passed. This only matters for collections with drafts/versions enabled.
Source
Thrown at packages/payload/src/collections/operations/local/countVersions.ts:59
* If you set `overrideAccess` to `false`, you can pass a user to use against the access control checks.
*/
user?: null | User
/**
* A filter [query](https://payloadcms.com/docs/queries/overview)
*/
where?: Where
}
export async function countVersionsLocal<TSlug extends CollectionSlug>(
payload: Payload,
options: CountVersionsOptions<TSlug>,
): Promise<{ totalDocs: number }> {
const { collection: collectionSlug, disableErrors, overrideAccess = true, where } = options
const collection = payload.collections[collectionSlug]
if (!collection) {
throw new APIError(
`The collection with slug ${String(collectionSlug)} can't be found. Count Versions Operation.`,
)
}
return countVersionsOperation<TSlug>({
collection,
disableErrors,
overrideAccess,
req: await createLocalReq(options as CreateLocalReqOptions, payload),
where,
})
}
View on GitHub (pinned to 00c58b35c0)
Solutions
- Confirm the slug matches the collection config's `slug` property exactly (watch singular/plural).
- Verify `versions: true` (or a drafts config) is set on that collection, since countVersions only applies to versioned collections.
- Print `Object.keys(payload.collections)` to verify the slug is registered at call time.
- Drop `as CollectionSlug` casts on runtime-built strings.
Example fix
// before
await payload.countVersions({ collection: 'page' }) // real slug is 'pages'
// after
await payload.countVersions({ collection: 'pages' }) 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, 'pages')
await payload.countVersions({ collection: 'pages' }) 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.countVersions({ collection: slug })
} Try / catch
try {
await payload.countVersions({ collection: slug })
} catch (err) {
if (err instanceof APIError && /Count Versions Operation/.test(err.message)) {
// unknown slug — log registered slugs and fix caller
} else throw err
} Prevention
- Keep a single source of truth for collection slug constants.
- Confirm versions are enabled on the target collection.
- Avoid `as CollectionSlug` casts on runtime-built strings.
When it happens
Trigger: Calling `payload.countVersions({ collection: 'post' })` against a misspelled slug, or counting versions of a collection whose `versions` config was disabled (so the slug may still be valid but no versions table exists), or using a slug from an older config after a rename.
Common situations: Renaming a versioned collection without updating the analytics/dashboard code that counts versions; calling countVersions in a migration before the collection is registered; asserting a dynamic slug with `as CollectionSlug`.
Related errors
- The collection with slug ${String(collectionSlug)} can't be
- The collection with slug ${String(collectionSlug)} can't be
- The collection with slug ${String(collectionSlug)} can't be
- The collection with slug ${String(collectionSlug)} can't be
- The collection with slug ${String(collectionSlug)} can't be
AI-assisted analysis of payloadcms/payload@00c58b35c0 (2026-08-12).
Data as JSON: /api/errors/4b23e08ff985b1bf.
Report an issue: GitHub.