{"record":{"id":"c8c0fa6283928b78","repo":"payloadcms/payload","slug":"the-collection-with-slug-string-collectionslug-c8c0fa","errorCode":null,"errorMessage":"The collection with slug ${String(collectionSlug)} can't be found. Find By ID Operation.","messagePattern":"The collection with slug (.+?) can't be found\\. Find By ID Operation\\.","errorType":"http","errorClass":"APIError","httpStatus":500,"severity":"error","filePath":"packages/payload/src/collections/operations/local/findByID.ts","lineNumber":152,"sourceCode":"    currentDepth,\n    data,\n    depth,\n    disableErrors = false,\n    draft = false,\n    flattenLocales,\n    includeLockStatus,\n    joins,\n    overrideAccess = true,\n    populate,\n    select,\n    showHiddenFields,\n    trash = false,\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. Find By ID Operation.`,\n    )\n  }\n\n  return findByIDOperation<TSlug, TDisableErrors, TSelect>({\n    id,\n    collection,\n    currentDepth,\n    data,\n    depth,\n    disableErrors,\n    draft,\n    flattenLocales,\n    includeLockStatus,\n    joins,\n    overrideAccess,\n    populate,\n    req: await createLocalReq(options as CreateLocalReqOptions, payload),","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/payloadcms/payload/blob/00c58b35c0ed348ddc22daabf467b139727214fd/packages/payload/src/collections/operations/local/findByID.ts#L134-L170","documentation":"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.","triggerScenarios":"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.","commonSituations":"Slug typo; collection removed but relationship fields still reference it; `as CollectionSlug` cast on request input; calling before `payload.init()`.","solutions":["Verify the slug against the collection config and `Object.keys(payload.collections)`.","If the slug comes from a request parameter, allowlist it against registered slugs.","Ensure `payload.init()` resolved.","Remove `as CollectionSlug` casts on untrusted values."],"exampleFix":"// before\nawait payload.findByID({ collection: 'category', id }) // slug is 'categories'\n\n// after\nawait payload.findByID({ collection: 'categories', id })","handlingStrategy":"validation","validationCode":"function assertCollectionSlug(payload: Payload, slug: string): void {\n  if (!(slug in payload.collections)) {\n    throw new Error(`Unknown collection slug '${slug}'`)\n  }\n}\nassertCollectionSlug(payload, 'categories')\nawait payload.findByID({ collection: 'categories', id })","typeGuard":"const slugIsRegistered = (payload: Payload, slug: string): slug is CollectionSlug =>\n  slug in (payload.collections as Record<string, unknown>)\n\nif (slugIsRegistered(payload, slug)) {\n  await payload.findByID({ collection: slug, id })\n}","tryCatchPattern":"try {\n  await payload.findByID({ collection: slug, id })\n} catch (err) {\n  if (err instanceof APIError && /Find By ID Operation/.test(err.message)) {\n    // unknown slug — correct it\n  } else throw err\n}","preventionTips":["Allowlist request-supplied slugs against payload.collections.","Keep slug constants in sync with config.","Avoid `as CollectionSlug` casts on untrusted input."],"tags":["local-api","collection","slug","find","configuration"],"backgroundTag":null,"analyzedSha":"00c58b35c0ed348ddc22daabf467b139727214fd","analyzedAt":"2026-08-12T20:45:03.758Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}