-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Description
Describe the Bug
I’ve encountered an issue using globals with the MongoDB adapter.
When a global document does not exist in the globals collection, Payload returns an empty object from GET /api/globals/<global_slug>, and all default values defined in the global config are ignored.
Expected behavior:
If the global document does not yet exist in the database, the response should include the default values defined in the global config.
Actual behavior:
The response returns {}.
Example request:
GET /api/globals/<global_slug>
Actual response:
{}Workaround:
Manually insert an empty document for the global in the globals collection:
{
"_id": { "$oid": "<ObjectId>" },
"globalType": "<global_slug>"
}After doing this, the default values are applied as expected.
Notes:
- I observed this with the MongoDB adapter. I haven’t tested with other database adapters.
Link to the code that reproduces this issue
https://github.com/Fitch24/payload/tree/reproduction/globals-defaults
Reproduction Steps
Clone reproduction repo and:
pnpm installpnpm dev _communitycurl --location 'http://localhost:3000/api/globals/menu'
Expected response:
{
"globalText": "This field must have default value if no \"global\" document exists in db"
}Actual response:
{}Which area(s) are affected? (Select all that apply)
db: mongodb
Environment Info
Binaries:
Node: 22.20.0
npm: N/A
Yarn: N/A
pnpm: N/A
Relevant Packages:
payload: 3.62.1
Operating System:
Platform: win32
Arch: x64
Version: Windows 10 Pro
Available memory (MB): 24449
Available CPU cores: 6