payloadcms/payload · error · APIError

No collection was specified

Error message

No collection was specified

What it means

Thrown by `getRequestCollection` when `req.routeParams.collection` is absent or not a string. This utility resolves the target collection from the request route parameters on every collection-scoped REST endpoint.

Source

Thrown at packages/payload/src/utilities/getRequestEntity.ts:11

import type { Collection } from '../collections/config/types.js'
import type { SanitizedGlobalConfig } from '../globals/config/types.js'
import type { PayloadRequest } from '../types/index.js'

import { APIError } from '../errors/APIError.js'

export const getRequestCollection = (req: PayloadRequest): Collection => {
  const collectionSlug = req.routeParams?.collection

  if (typeof collectionSlug !== 'string') {
    throw new APIError(`No collection was specified`, 400)
  }

  const collection = req.payload.collections[collectionSlug]

  if (!collection) {
    throw new APIError(`Collection with the slug ${collectionSlug} was not found`, 404)
  }

  return collection
}

export const getRequestCollectionWithID = <T extends boolean>(
  req: PayloadRequest,
  {
    disableSanitize,
    optionalID,
  }: {
    disableSanitize?: T

View on GitHub (pinned to 00c58b35c0)

Solutions

  1. Ensure the request URL includes the collection slug segment and the route pattern captures it into `routeParams.collection`.
  2. If calling the REST layer programmatically, set `req.routeParams = { collection: 'yourSlug' }` on the synthetic request.
  3. Verify no middleware mutates or clears `req.routeParams` before Payload processes the request.
  4. Use the Local API (`payload.find`, `payload.create`, etc.) instead of constructing raw requests when possible.

Example fix

// before -- custom handler forwards without routeParams
app.post('/api/media', (req, res) => payloadRestHandler(req, res)) // no routeParams.collection

// after
app.post('/api/:collection', (req, res, next) => {
  req.routeParams = { collection: req.params.collection }
  next()
})
Defensive patterns

Strategy: validation

Validate before calling

// Before forwarding to Payload REST, ensure routeParams.collection is set
if (typeof req.routeParams?.collection !== 'string') {
  throw new Error('Missing collection slug in route params')
}

Type guard

const hasCollectionParam = (req) =>
  typeof req.routeParams?.collection === 'string'

Try / catch

try {
  collection = getRequestCollection(req)
} catch (e) {
  if (e instanceof APIError && e.message === 'No collection was specified') {
    // set req.routeParams.collection and retry, or return 400
  } else throw e
}

Prevention

When it happens

Trigger: A request reaches a collection-resolution path (e.g. via `getRequestCollection` or `getRequestCollectionWithID`) but the route was matched without populating `routeParams.collection`. This typically indicates a misconfigured custom route, a middleware that strips route params, or an internal call missing the param.

Common situations: A custom Express/Next handler forwards to Payload REST layer without setting route params; a middleware or proxy rewrote the URL and dropped the collection segment; an internal Local API call constructed a synthetic `PayloadRequest` without `routeParams`; a misconfigured custom route pattern that does not capture the collection slug.

Related errors


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