Skip to content

Commit 7922d66

Browse files
authored
fix(db-mongodb): properly handle document notfound cases for update and delete operations (#11267)
In the `findOne` db operation, we return `null` if the document was not found. For single-document delete and update operations, if the document you wanted to update is not found, the following runtime error is thrown instead: `Cannot read properties of null (reading '_id')`. This PR correctly handles these cases and returns `null` from the db method, just like the `findOne` operation.
1 parent 0d7cf3f commit 7922d66

File tree

6 files changed

+36
-1
lines changed

6 files changed

+36
-1
lines changed

packages/db-mongodb/src/deleteOne.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export const deleteOne: DeleteOne = async function deleteOne(
2929
where,
3030
})
3131

32-
const doc = await Model.findOneAndDelete(query, options).lean()
32+
const doc = await Model.findOneAndDelete(query, options)?.lean()
33+
34+
if (!doc) {
35+
return null
36+
}
3337

3438
let result: Document = JSON.parse(JSON.stringify(doc))
3539

packages/db-mongodb/src/updateGlobal.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export const updateGlobal: UpdateGlobal = async function updateGlobal(
3737

3838
result = await Model.findOneAndUpdate({ globalType: slug }, sanitizedData, options)
3939

40+
if (!result) {
41+
return null
42+
}
43+
4044
result = JSON.parse(JSON.stringify(result))
4145

4246
// custom id type reset

packages/db-mongodb/src/updateGlobalVersion.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ export async function updateGlobalVersion<T extends TypeWithID>(
5555

5656
const doc = await VersionModel.findOneAndUpdate(query, sanitizedData, options)
5757

58+
if (!doc) {
59+
return null
60+
}
61+
5862
const result = JSON.parse(JSON.stringify(doc))
5963

6064
const verificationToken = doc._verificationToken

packages/db-mongodb/src/updateOne.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export const updateOne: UpdateOne = async function updateOne(
5151
handleError({ collection, error, req })
5252
}
5353

54+
if (!result) {
55+
return null
56+
}
57+
5458
result = JSON.parse(JSON.stringify(result))
5559
result.id = result._id
5660
result = sanitizeInternalFields(result)

packages/db-mongodb/src/updateVersion.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export const updateVersion: UpdateVersion = async function updateVersion(
5353

5454
const doc = await VersionModel.findOneAndUpdate(query, sanitizedData, options)
5555

56+
if (!doc) {
57+
return null
58+
}
59+
5660
const result = JSON.parse(JSON.stringify(doc))
5761

5862
const verificationToken = doc._verificationToken

packages/payload/src/database/types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ export type UpdateGlobalVersionArgs<T = TypeWithID> = {
305305
}
306306
)
307307

308+
/**
309+
* @todo type as Promise<TypeWithVersion<T> | null> in 4.0
310+
*/
308311
export type UpdateGlobalVersion = <T extends TypeWithID = TypeWithID>(
309312
args: UpdateGlobalVersionArgs<T>,
310313
) => Promise<TypeWithVersion<T>>
@@ -332,6 +335,9 @@ export type UpdateGlobalArgs<T extends Record<string, unknown> = any> = {
332335
select?: SelectType
333336
slug: string
334337
}
338+
/**
339+
* @todo type as Promise<T | null> in 4.0
340+
*/
335341
export type UpdateGlobal = <T extends Record<string, unknown> = any>(
336342
args: UpdateGlobalArgs<T>,
337343
) => Promise<T>
@@ -410,6 +416,9 @@ export type UpdateVersionArgs<T = TypeWithID> = {
410416
}
411417
)
412418

419+
/**
420+
* @todo type as Promise<TypeWithVersion<T> | null> in 4.0
421+
*/
413422
export type UpdateVersion = <T extends TypeWithID = TypeWithID>(
414423
args: UpdateVersionArgs<T>,
415424
) => Promise<TypeWithVersion<T>>
@@ -448,6 +457,9 @@ export type UpdateOneArgs = {
448457
}
449458
)
450459

460+
/**
461+
* @todo type as Promise<Document | null> in 4.0
462+
*/
451463
export type UpdateOne = (args: UpdateOneArgs) => Promise<Document>
452464

453465
export type UpsertArgs = {
@@ -470,6 +482,9 @@ export type DeleteOneArgs = {
470482
where: Where
471483
}
472484

485+
/**
486+
* @todo type as Promise<Document | null> in 4.0
487+
*/
473488
export type DeleteOne = (args: DeleteOneArgs) => Promise<Document>
474489

475490
export type DeleteManyArgs = {

0 commit comments

Comments
 (0)