fix(relation refs): buildRefs 跳过空串关联 id,避免可选 Relation 存 "" 时 $in 转 ObjectId 抛 CastError 整页 500

v0.7.11 误改在废弃的 _label/populateItems(无调用方)上:列表实际走 list()->buildRefs()。
本次回退 populateItems,改在真正活路径 collectOne。bump 0.7.12
This commit is contained in:
2026-07-29 15:34:13 +08:00
parent 31b723839e
commit 19afdf3615
3 changed files with 5 additions and 5 deletions
+2 -1
View File
@@ -292,7 +292,8 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = 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)
+2 -3
View File
@@ -79,10 +79,9 @@ export class MongoShrimpCrud<T> implements ShrimpCrud<T> {
const allRefIds = new Set<string>()
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))
}
}