{"record":{"id":"5e98c78e75ae881c","repo":"payloadcms/payload","slug":"field-must-be-specified","errorCode":null,"errorMessage":"field must be specified","messagePattern":"field must be specified","errorType":"http","errorClass":"APIError","httpStatus":400,"severity":"warning","filePath":"packages/payload/src/collections/endpoints/findDistinct.ts","lineNumber":17,"sourceCode":"import { status as httpStatus } from 'http-status'\n\nimport type { PayloadHandler } from '../../config/types.js'\n\nimport { APIError } from '../../errors/APIError.js'\nimport { getRequestCollection } from '../../utilities/getRequestEntity.js'\nimport { headersWithCors } from '../../utilities/headersWithCors.js'\nimport { parseParams } from '../../utilities/parseParams/index.js'\nimport { findDistinctOperation } from '../operations/findDistinct.js'\n\nexport const findDistinctHandler: PayloadHandler = async (req) => {\n  const collection = getRequestCollection(req)\n\n  const { depth, field, limit, page, sort, trash, where } = parseParams(req.query)\n\n  if (!field) {\n    throw new APIError('field must be specified', httpStatus.BAD_REQUEST)\n  }\n\n  const result = await findDistinctOperation({\n    collection,\n    depth,\n    field,\n    limit,\n    page,\n    req,\n    sort,\n    trash,\n    where,\n  })\n\n  return Response.json(result, {\n    headers: headersWithCors({\n      headers: new Headers(),\n      req,","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/payloadcms/payload/blob/00c58b35c0ed348ddc22daabf467b139727214fd/packages/payload/src/collections/endpoints/findDistinct.ts#L1-L35","documentation":"The `findDistinctHandler` REST handler parses query params; if `field` is absent/empty it throws an `APIError` (400). The distinct endpoint must know which field to deduplicate values on, so a missing `field` is a malformed request.","triggerScenarios":"`GET /api/{collection}/distinct` without a `field` query param, or with `field=` (empty string). The parsed `field` is falsy.","commonSituations":"Client forgetting the `field` param; URL builder dropping it; typo such as `?fields=` (plural).","solutions":["Add `?field=<fieldName>` to the request.","Validate the param in the client before issuing the request."],"exampleFix":"// before\nfetch('/api/posts/distinct')\n// after\nfetch(`/api/posts/distinct?field=${encodeURIComponent('category')}`)","handlingStrategy":"validation","validationCode":"function buildDistinctUrl(collection, field) {\n  if (!field) throw new Error('field query param is required')\n  return `/api/${collection}/distinct?field=${encodeURIComponent(field)}`\n}","typeGuard":"function hasFieldParam(query): query is { field: string } {\n  return typeof query?.field === 'string' && query.field.length > 0\n}","tryCatchPattern":null,"preventionTips":["Always pass `field` to the distinct endpoint.","Centralize distinct URL building so the param can't be dropped."],"tags":["api","distinct","validation","query-params"],"backgroundTag":null,"analyzedSha":"00c58b35c0ed348ddc22daabf467b139727214fd","analyzedAt":"2026-08-12T20:45:03.758Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}