diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index b13ec22cfd..f6b75aca05 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -19,6 +19,7 @@ import { breakRowIdField, convertRowId, generateRowIdField, + getPrimaryDisplay, isRowId, isSQL, } from "../../../integrations/utils" @@ -391,7 +392,10 @@ export class ExternalRequest { } } relatedRow = processFormulas(linkedTable, relatedRow) - const relatedDisplay = display ? relatedRow[display] : undefined + let relatedDisplay + if (display) { + relatedDisplay = getPrimaryDisplay(relatedRow[display]) + } row[relationship.column][key] = { primaryDisplay: relatedDisplay || "Invalid display column", _id: relatedRow._id, diff --git a/packages/server/src/integrations/utils.ts b/packages/server/src/integrations/utils.ts index de8b318bb1..cc3caa6d5b 100644 --- a/packages/server/src/integrations/utils.ts +++ b/packages/server/src/integrations/utils.ts @@ -328,3 +328,27 @@ export function finaliseExternalTables( .reduce((r, [k, v]) => ({ ...r, [k]: v }), {}) return { tables: finalTables, errors } } + +/** + * Checks if the provided input is an object, but specifically not a date type object. + * Used during coercion of types and relationship handling, dates are considered valid + * and can be used as a display field, but objects and arrays cannot. + * @param testValue an unknown type which this function will attempt to extract + * a valid primary display string from. + */ +export function getPrimaryDisplay(testValue: unknown): string | undefined { + if (testValue instanceof Date) { + return testValue.toISOString() + } + if ( + Array.isArray(testValue) && + testValue[0] && + typeof testValue[0] !== "object" + ) { + return testValue.join(", ") + } + if (typeof testValue === "object") { + return undefined + } + return testValue as string +}