fix(crud): add/edit 返回普通对象(toObject/lean),不再抖出 mongoose Document 内部

add 用 create 返回 Document、edit 用 findByIdAndUpdate 返回 Document,下游 processResItem
的 {...item} 会把 $__/_doc/$isNew 等内部属性展开、真数据埋进 _doc。改:add 转 toObject、
edit 加 .lean()(与 list/get 一致)。响应回归干净 POJO。
This commit is contained in:
2026-06-30 14:06:40 +08:00
parent c264ec6673
commit d12877f846
2 changed files with 4 additions and 3 deletions
+3 -2
View File
@@ -107,10 +107,11 @@ export class MongoShrimpCrud<T> implements ShrimpCrud<T> {
return await this.model.findById(id).lean() as T | null
}
async add(item: Omit<T, '_id'>): Promise<T> {
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<T>): Promise<T | null> {
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<boolean> {
const result = await this.model.findByIdAndDelete(id)