From e19efc0bbaa47de4faf2887dbc4c6c2160072047 Mon Sep 17 00:00:00 2001 From: mertmit Date: Sat, 6 Jan 2024 08:47:48 +0000 Subject: [PATCH] fix: post-processing for user field --- packages/nocodb/src/db/BaseModelSqlv2.ts | 50 ++++++++++++++---------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index f6d142e49d..c8d36bc8c9 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -4719,7 +4719,7 @@ class BaseModelSqlv2 { await this.model.getColumns(); } - const userColumns = []; + let userColumns = []; const columns = childTable ? childTable.columns : this.model.columns; @@ -4743,6 +4743,19 @@ class BaseModelSqlv2 { } } + // filter user columns that are not present in data + if (userColumns.length) { + if (Array.isArray(data)) { + const row = data[0]; + if (row) { + userColumns = userColumns.filter((col) => col.id in row); + } + } else { + userColumns = userColumns.filter((col) => col.id in data); + } + } + + // process user columns that are present in data if (userColumns.length) { const baseUsers = await BaseUser.getUsersList({ base_id: childTable ? childTable.base_id : this.model.base_id, @@ -4767,29 +4780,26 @@ class BaseModelSqlv2 { ) { try { if (d) { - for (const col of userColumns) { - if (d[col.id] && d[col.id].length) { - d[col.id] = d[col.id].split(','); - } else { - d[col.id] = null; - } + const availableUserColumns = userColumns.filter( + (col) => d[col.id] && d[col.id].length, + ); + for (const col of availableUserColumns) { + d[col.id] = d[col.id].split(','); - if (d[col.id]?.length) { - d[col.id] = d[col.id].map((fid) => { - const { id, email, display_name } = baseUsers.find( - (u) => u.id === fid, - ); - return { - id, - email, - display_name: display_name?.length ? display_name : null, - }; - }); - } + d[col.id] = d[col.id].map((fid) => { + const { id, email, display_name } = baseUsers.find( + (u) => u.id === fid, + ); + return { + id, + email, + display_name: display_name?.length ? display_name : null, + }; + }); // CreatedBy and LastModifiedBy are always singular if ([UITypes.CreatedBy, UITypes.LastModifiedBy].includes(col.uidt)) { - d[col.id] = d[col.id]?.[0] ?? null; + d[col.id] = d[col.id][0]; } } }