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.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user