payloadcms/payload · error · APIError

Fields are not initialized.

Error message

Fields are not initialized.

What it means

Thrown by the MongoDB query builder after the global/collection lookup block when `fields` is still `null` — meaning neither `globalSlug` nor `collectionSlug` was supplied (or both were falsy). The param parser requires field definitions to operate, so it refuses to build a query rather than guess.

Source

Thrown at packages/db-mongodb/src/queries/getBuildQueryPlugin.ts:60

          if (!globalConfig) {
            throw new APIError(`Global with the slug ${globalSlug} was not found`)
          }

          fields = globalConfig.flattenedFields
        }
        if (collectionSlug) {
          const collectionConfig = payload.collections[collectionSlug]?.config

          if (!collectionConfig) {
            throw new APIError(`Collection with the slug ${globalSlug} was not found`)
          }

          fields = collectionConfig.flattenedFields
        }
      }

      if (fields === null) {
        throw new APIError('Fields are not initialized.')
      }

      const result = await parseParams({
        collectionSlug,
        fields,
        globalSlug,
        locale,
        parentIsLocalized: false,
        payload,
        where,
      })

      return result
    }
    modifiedSchema.statics.buildQuery = schemaBuildQuery
  }
}

View on GitHub (pinned to 00c58b35c0)

Solutions

  1. Ensure every query call specifies either `collectionSlug` or `globalSlug`.
  2. If you call the plugin/adapter internals directly, always forward the originating slug.
  3. Audit middleware/REST layers to confirm the slug is propagated into the query args.
  4. On upgrade, re-check the BuildQueryArgs interface for new required fields.

Example fix

// before
await adapter.query({ where, payload }) // no slug
// after
await adapter.query({ collectionSlug: 'Posts', where, payload })
Defensive patterns

Strategy: validation

Validate before calling

function assertQueryContext(args) {
  if (!args.collectionSlug && !args.globalSlug)
    throw new Error('Either collectionSlug or globalSlug is required')
}

Type guard

const hasQueryTarget = (a) => Boolean(a?.collectionSlug) || Boolean(a?.globalSlug)

Try / catch

try { await adapter.query(args) }
catch (e) { if (/Fields are not initialized/.test(e.message)) throw new Error('Query missing collection/global context') else throw e }

Prevention

When it happens

Trigger: `getBuildQueryPlugin` invoked with neither `globalSlug` nor `collectionSlug` set; both passed as undefined/empty string; an internal caller or custom adapter method forgot to forward the originating slug into BuildQueryArgs.

Common situations: Custom adapter code or plugin calling the query builder directly without context; a regression after a Payload upgrade that changed the BuildQueryArgs shape; middleware that strips the slug before it reaches the query layer.

Related errors


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