From 19afdf36154810d4bc381e828fb6a65b632a5c6d Mon Sep 17 00:00:00 2001 From: wc24 Date: Wed, 29 Jul 2026 15:34:13 +0800 Subject: [PATCH] =?UTF-8?q?fix(relation=20refs):=20buildRefs=20=E8=B7=B3?= =?UTF-8?q?=E8=BF=87=E7=A9=BA=E4=B8=B2=E5=85=B3=E8=81=94=20id=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E5=8F=AF=E9=80=89=20Relation=20=E5=AD=98=20"?= =?UTF-8?q?"=20=E6=97=B6=20$in=20=E8=BD=AC=20ObjectId=20=E6=8A=9B=20CastEr?= =?UTF-8?q?ror=20=E6=95=B4=E9=A1=B5=20500?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit v0.7.11 误改在废弃的 _label/populateItems(无调用方)上:列表实际走 list()->buildRefs()。 本次回退 populateItems,改在真正活路径 collectOne。bump 0.7.12 --- package.json | 2 +- src/Shrimp/ShrimpTableModule.ts | 3 ++- src/forMongo/MongoShrimpCrud.ts | 5 ++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 74f8286..481869d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "shrimp-server", - "version": "0.7.11", + "version": "0.7.12", "description": "Shrimp 框架后端 — 声明式字段驱动的 Node.js 管理后台框架(Express + Mongoose + JWT)", "main": "dist/main/index.js", "types": "dist/main/index.d.ts", diff --git a/src/Shrimp/ShrimpTableModule.ts b/src/Shrimp/ShrimpTableModule.ts index 4b8703e..9dfa870 100644 --- a/src/Shrimp/ShrimpTableModule.ts +++ b/src/Shrimp/ShrimpTableModule.ts @@ -292,7 +292,8 @@ export class ShrimpTableModule = ITableCrudProto private collectOne(f: IFieldConfig, v: any, add: (moduleKey: string, labelKey: string, id: any) => void): void { if (v == null) return if (f.type === "relation" && f.relation) { - ;(Array.isArray(v) ? v : [v]).forEach((id: any) => id != null && add(f.relation!.moduleKey, f.relation!.labelKey, id)) + // 跳过空串:可选 Relation 常存 "",混进 { _id: { $in } } 会让 Mongoose 转 ObjectId 抛 CastError、整页 500。 + ;(Array.isArray(v) ? v : [v]).forEach((id: any) => id != null && id !== "" && add(f.relation!.moduleKey, f.relation!.labelKey, id)) } else if (f.type === ShrimpFieldTypes.Tab && Array.isArray(f.tabs) && typeof v === "object") { const branch = f.tabs.find(t => t.key === v.type) if (branch) for (const c of branch.fields) this.collectOne(c, v[c.key], add) diff --git a/src/forMongo/MongoShrimpCrud.ts b/src/forMongo/MongoShrimpCrud.ts index b69d044..cc7ff29 100644 --- a/src/forMongo/MongoShrimpCrud.ts +++ b/src/forMongo/MongoShrimpCrud.ts @@ -79,10 +79,9 @@ export class MongoShrimpCrud implements ShrimpCrud { const allRefIds = new Set() for (const item of items) { const val = (item as any)[pop.key] - // 跳过空串:可选 Relation 常存 "",混进 { _id: { $in } } 会让 Mongoose 转 ObjectId 抛 CastError、整页 500。 if (Array.isArray(val)) { - val.forEach((id: any) => { if (id != null && id !== "") allRefIds.add(String(id)) }) - } else if (val != null && val !== "") { + val.forEach((id: any) => { if (id != null) allRefIds.add(String(id)) }) + } else if (val != null) { allRefIds.add(String(val)) } }