test: guard against null values in custom toCSV functions (#12938)

### What?

Fixes a crash when exporting documents to CSV if a custom `toCSV`
function tries to access properties on a `null` value.

### Why?

In some cases (especially with Postgres), fields like relationships may
be explicitly `null` if unset. Custom `toCSV` functions that assume the
value is always defined would throw a `TypeError` when attempting to
access nested properties like `value.id`.

### How?

Added a null check in the custom `toCSV` implementation for
`customRelationship`, ensuring the field is an object before accessing
its properties.

This prevents the export from failing and makes custom field transforms
more resilient to missing or optional values.
This commit is contained in:
Patrik
2025-06-25 11:45:09 -04:00
committed by GitHub
parent 6d768748a0
commit 1cdec861cd

View File

@@ -44,9 +44,11 @@ export const Pages: CollectionConfig = {
relationTo: 'users', relationTo: 'users',
custom: { custom: {
'plugin-import-export': { 'plugin-import-export': {
toCSV: ({ value, columnName, row, siblingDoc, doc }) => { toCSV: ({ value, columnName, row }) => {
row[`${columnName}_id`] = value.id if (value && typeof value === 'object' && 'id' in value && 'email' in value) {
row[`${columnName}_email`] = value.email row[`${columnName}_id`] = (value as { id: number | string }).id
row[`${columnName}_email`] = (value as { email: string }).email
}
}, },
}, },
}, },