From 34da8a8d0dc6ccac434c9c75198fb97ea2cf9adf Mon Sep 17 00:00:00 2001 From: Pranav C Date: Tue, 25 Jun 2024 11:25:34 +0000 Subject: [PATCH] fix: handle when HM lookups to BT --- packages/nocodb/src/db/BaseModelSqlv2.ts | 36 ++++++++++++++++++------ 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index c614230251..7f119be7d0 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -5973,7 +5973,11 @@ class BaseModelSqlv2 { ); if (btData) { if (typeof btData[col.id] === 'object') { - for (const k of Object.keys(btData[col.id])) { + for (const k of Object.keys( + Array.isArray(btData[col.id]) + ? btData[col.id][0] || {} + : btData[col.id], + )) { const btAlias = idToAliasMap[k]; if (!btAlias) { idToAliasPromiseMap[k] = Column.get(this.context, { @@ -6010,13 +6014,29 @@ class BaseModelSqlv2 { if (alias) { if (btMap[key]) { if (value && typeof value === 'object') { - const tempObj = {}; - Object.entries(value).forEach(([k, v]) => { - const btAlias = idToAliasMap[k]; - if (btAlias) { - tempObj[btAlias] = v; - } - }); + let tempObj: Record | Record[]; + + // if array of values then handle by using map, it will be an array when it's HM Lookup to a BT + if (Array.isArray(value)) { + tempObj = value.map((arrVal) => { + const obj = {}; + Object.entries(arrVal).forEach(([k, val]) => { + const btAlias = idToAliasMap[k]; + if (btAlias) { + obj[btAlias] = val; + } + }); + return obj; + }); + } else { + tempObj = {}; + Object.entries(value).forEach(([k, v]) => { + const btAlias = idToAliasMap[k]; + if (btAlias) { + tempObj[btAlias] = v; + } + }); + } item[alias] = tempObj; } else { item[alias] = value;