From d12877f846701a04b5c3e8667821d461cde0ec6e Mon Sep 17 00:00:00 2001 From: wc24 Date: Tue, 30 Jun 2026 14:06:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(crud):=20add/edit=20=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E6=99=AE=E9=80=9A=E5=AF=B9=E8=B1=A1(toObject/lean),=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E6=8A=96=E5=87=BA=20mongoose=20Document=20=E5=86=85?= =?UTF-8?q?=E9=83=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add 用 create 返回 Document、edit 用 findByIdAndUpdate 返回 Document,下游 processResItem 的 {...item} 会把 $__/_doc/$isNew 等内部属性展开、真数据埋进 _doc。改:add 转 toObject、 edit 加 .lean()(与 list/get 一致)。响应回归干净 POJO。 --- package.json | 2 +- src/forMongo/MongoShrimpCrud.ts | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 068bc90..9cbf471 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "shrimp-server", - "version": "0.7.0", + "version": "0.7.1", "description": "Shrimp 框架后端 — 声明式字段驱动的 Node.js 管理后台框架(Express + Mongoose + JWT)", "main": "dist/main/index.js", "types": "dist/main/index.d.ts", diff --git a/src/forMongo/MongoShrimpCrud.ts b/src/forMongo/MongoShrimpCrud.ts index b8dd023..cc7ff29 100644 --- a/src/forMongo/MongoShrimpCrud.ts +++ b/src/forMongo/MongoShrimpCrud.ts @@ -107,10 +107,11 @@ export class MongoShrimpCrud implements ShrimpCrud { return await this.model.findById(id).lean() as T | null } async add(item: Omit): Promise { - return await this.model.create(item) + const doc = await this.model.create(item) + return doc.toObject() as T // 转普通对象,否则下游 {...item} 展开的是 mongoose Document 内部($__/_doc/$isNew) } async edit(id: string, item: Partial): Promise { - return await this.model.findByIdAndUpdate(id, item, { new: true }) + return await this.model.findByIdAndUpdate(id, item, { new: true }).lean() as T | null } async delete(id: string): Promise { const result = await this.model.findByIdAndDelete(id)