Skip to content

Array fields in Globals crash with 'Cannot read properties of null (reading id)' in handleDocumentLocking #14915

@jellologic

Description

@jellologic

Description

When adding items to an array field in a Global config, the UI crashes with:

Error in queued function: TypeError: Cannot read properties of null (reading 'id')

This does not occur with array fields in Collections - only Globals.

Payload Version

3.68.3

Steps to Reproduce

  1. Create a Global with an array field:
export const GlobalSettings: GlobalConfig = {
  slug: "global-settings",
  fields: [
    {
      name: "alternateName",
      type: "array",
      label: "Alternate Names",
      fields: [
        {
          name: "name",
          type: "text",
          label: "Name",
        },
      ],
    },
  ],
};
  1. Navigate to /admin/globals/global-settings
  2. Click "Add Alternate Name" to add an array item
  3. Error occurs immediately

Root Cause

The bug is in @payloadcms/ui/dist/views/Edit/index.js in the handleDocumentLocking callback (around line 186-187):

if (lockedState) {
  const lockedUserID = typeof lockedState.user === 'string' || typeof lockedState.user === 'number' 
    ? lockedState.user 
    : lockedState.user.id;  // BUG: crashes if lockedState.user is null

The code checks if lockedState is truthy, but assumes lockedState.user is also defined. For Globals, lockedState can exist while lockedState.user is null, causing the crash.

Suggested Fix

Add null checking for lockedState.user:

if (lockedState && lockedState.user != null) {
  const lockedUserID = typeof lockedState.user === 'string' || typeof lockedState.user === 'number'
    ? lockedState.user
    : (lockedState.user && typeof lockedState.user === 'object' ? lockedState.user.id : null);
  if (lockedUserID == null) return;
  // ... rest of the function
}

Workaround

Apply a patch to @payloadcms/ui with the fix above using pnpm's patchedDependencies or similar.

Environment

  • Node: 20.x
  • Database: PostgreSQL (via @payloadcms/db-postgres)
  • OS: Linux

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions