payload icon indicating copy to clipboard operation
payload copied to clipboard

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

Open jellologic opened this issue 11 months ago • 4 comments

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

jellologic avatar Dec 14 '25 18:12 jellologic

Same problem here – error is so generic it was impossible to debug...

wecode-swiss-bruno avatar Jan 14 '26 16:01 wecode-swiss-bruno

I can take a look at this. The fix seems straightforward - adding a null-check for lockedState.user before accessing .id.

Will reproduce locally and submit a PR.

RajeshKumar11 avatar Jan 19 '26 15:01 RajeshKumar11

🚀 This is included in version v3.74.0

github-actions[bot] avatar Jan 30 '26 16:01 github-actions[bot]

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.

github-actions[bot] avatar Feb 07 '26 05:02 github-actions[bot]