From 6dc61ae6429359f0cce824ee2de243e380dab728 Mon Sep 17 00:00:00 2001 From: Sasha <64744993+r1tsuu@users.noreply.github.com> Date: Fri, 18 Apr 2025 21:43:53 +0300 Subject: [PATCH] fix(db-mongodb): fallback `version` when not selected (#12158) When doing `payload.db.queryDrafts` with `select` without `version`, or simply your select looks like: `select: { version: { nonExistingField: true } }` - the `queryDrafts` function will crash because it tries to access the `version` field. This PR adds a fallback. --- packages/db-mongodb/src/queryDrafts.ts | 2 +- test/database/int.spec.ts | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/db-mongodb/src/queryDrafts.ts b/packages/db-mongodb/src/queryDrafts.ts index e0e7a9e3ea..685bfa1ddb 100644 --- a/packages/db-mongodb/src/queryDrafts.ts +++ b/packages/db-mongodb/src/queryDrafts.ts @@ -166,7 +166,7 @@ export const queryDrafts: QueryDrafts = async function queryDrafts( for (let i = 0; i < result.docs.length; i++) { const id = result.docs[i].parent - result.docs[i] = result.docs[i].version + result.docs[i] = result.docs[i].version ?? {} result.docs[i].id = id } diff --git a/test/database/int.spec.ts b/test/database/int.spec.ts index 77e4d83091..ad45a88f40 100644 --- a/test/database/int.spec.ts +++ b/test/database/int.spec.ts @@ -2403,4 +2403,15 @@ describe('database', () => { payload.db.allowAdditionalKeys = false }) + + it('should not crash when the version field is not selected', async () => { + const customID = await payload.create({ collection: 'custom-ids', data: {} }) + const res = await payload.db.queryDrafts({ + collection: 'custom-ids', + where: { parent: { equals: customID.id } }, + select: { parent: true }, + }) + + expect(res.docs[0].id).toBe(customID.id) + }) })