diff --git a/app/src/displays/related-values/index.ts b/app/src/displays/related-values/index.ts index 98ecee0f5d..b7091b2334 100644 --- a/app/src/displays/related-values/index.ts +++ b/app/src/displays/related-values/index.ts @@ -97,11 +97,15 @@ export default defineDisplay({ types: ['alias', 'string', 'uuid', 'integer', 'bigInteger', 'json'], localTypes: ['m2m', 'm2o', 'o2m', 'translations', 'm2a', 'file', 'files'], fields: (options: Options | null, { field, collection }) => { - const { junctionCollection, relatedCollection, path } = getRelatedCollection(collection, field); - const fieldsStore = useFieldsStore(); - const primaryKeyField = fieldsStore.getPrimaryKeyFieldForCollection(relatedCollection); + const relatedCollectionData = getRelatedCollection(collection, field); - if (!relatedCollection) return []; + if (!relatedCollectionData) return []; + + const fieldsStore = useFieldsStore(); + + const { junctionCollection, relatedCollection, path } = relatedCollectionData; + + const primaryKeyField = fieldsStore.getPrimaryKeyFieldForCollection(relatedCollection); const fields = options?.template ? adjustFieldsForDisplays(getFieldsFromTemplate(options.template), junctionCollection ?? relatedCollection) diff --git a/app/src/displays/translations/index.ts b/app/src/displays/translations/index.ts index 4fdf7e66ba..0d7d2b7a9e 100644 --- a/app/src/displays/translations/index.ts +++ b/app/src/displays/translations/index.ts @@ -17,7 +17,7 @@ export default defineDisplay({ handler: (values, options, { collection, field }) => { if (!field || !collection || !Array.isArray(values)) return values; - const relatedCollections = getRelatedCollection(collection, field.field); + const relatedCollection = getRelatedCollection(collection, field.field); const fieldsStore = useFieldsStore(); const relationsStore = useRelationsStore(); @@ -34,9 +34,11 @@ export default defineDisplay({ (relation) => relation.collection === junction.collection && relation.field === junction.meta?.junction_field ); - const primaryKeyField = fieldsStore.getPrimaryKeyFieldForCollection(relatedCollections.relatedCollection); + if (!relatedCollection) return values; - if (!relatedCollections || !primaryKeyField || !relation?.related_collection) return values; + const primaryKeyField = fieldsStore.getPrimaryKeyFieldForCollection(relatedCollection.relatedCollection); + + if (!primaryKeyField || !relation?.related_collection) return values; const relatedPrimaryKeyField = fieldsStore.getPrimaryKeyFieldForCollection(relation.related_collection); @@ -61,7 +63,7 @@ export default defineDisplay({ const fields = fieldKeys.map((fieldKey) => { return { key: fieldKey, - field: fieldsStore.getField(relatedCollections.relatedCollection, fieldKey), + field: fieldsStore.getField(relatedCollection.relatedCollection, fieldKey), }; }); diff --git a/app/src/utils/get-related-collection.test.ts b/app/src/utils/get-related-collection.test.ts index 42fe45dde4..38f49f96dc 100644 --- a/app/src/utils/get-related-collection.test.ts +++ b/app/src/utils/get-related-collection.test.ts @@ -79,3 +79,11 @@ test('Returns M2O from related_collection rather than collection', () => { relatedCollection: 'articles', }); }); + +test('Returns null if no relation exists in the relationsStore', () => { + const relationsStore = useRelationsStore(); + + (relationsStore.getRelationsForField as Mock).mockReturnValue([]); + + expect(getRelatedCollection('users', 'favorite_article')).toEqual(null); +}); diff --git a/app/src/utils/get-related-collection.ts b/app/src/utils/get-related-collection.ts index 9548a41b2c..e8b5aba96d 100644 --- a/app/src/utils/get-related-collection.ts +++ b/app/src/utils/get-related-collection.ts @@ -14,12 +14,15 @@ export interface RelatedCollectionData { * * @param collection - Name of the current parent collection * @param field - Name of the relational field in the current collection - * @returns Related collection name(s) + * @returns Related collection name(s) or null if no related collection exists in the relationsStore */ -export function getRelatedCollection(collection: string, field: string): RelatedCollectionData { +export function getRelatedCollection(collection: string, field: string): RelatedCollectionData | null { const relationsStore = useRelationsStore(); const relations: Relation[] = relationsStore.getRelationsForField(collection, field); + + if (relations.length === 0) return null; + const localType = getLocalTypeForField(collection, field); const o2mTypes = ['o2m', 'm2m', 'm2a', 'translations', 'files'];