gpt4 book ai didi

serialization - 如何在使用 Typegoose 获取数据的同时使用类转换器序列化嵌套 js 响应?

转载 作者:行者123 更新时间:2023-12-04 02:38:27 30 4
gpt4 key购买 nike

我一直在尝试使用 Typegoose 使用 class-transformer 库完成 Mongodb 序列化部分的 NestJs 示例。在 https://docs.nestjs.com/techniques/serialization 给出的例子只展示了如何在 TypeORM 中使用序列化。我对 Typegoose 遵循了相同的过程。这是我迄今为止尝试过的。

// cat.domain.ts

import { prop } from '@typegoose/typegoose';

export class Cat {
@prop()
name: string;

@prop()
age: number;

@prop()
breed: string;
}


// cats.service.ts

@Injectable()
export class CatsService {
constructor(
@InjectModel(Cat) private readonly catModel: ReturnModelType<typeof Cat>,
) {}

findAll(): Observable<Cat[]> {
return from(this.catModel.find().exec());
}

findOne(id: string): Observable<Cat> {
return from(this.catModel.findById(id).exec());
}
...
}

// cat.response.ts

import { ObjectId } from 'mongodb';
import { Exclude, Transform } from 'class-transformer';

export class CatResponse {
@Transform(value => value.toString(), { toPlainOnly: true })
_id?: ObjectId;

name: string;

age: number;

@Exclude()
breed: string;

constructor(partial: Partial<CatResponse>) {
Object.assign(this, partial);
}
}

// cats.controller.ts

@Controller('cats')
@UseInterceptors(ClassSerializerInterceptor)
export class CatsController {
constructor(private readonly catsService: CatsService) {}

@Get()
findAll(): Observable<CatResponse[]> {
return this.catsService.findAll();
}

@Get(':id')
findOne(@Param() params: FindOneParamsDto): Observable<CatResponse> {
return this.catsService.findOne(params.id);
}
...
}

我尝试使用 id 在 Get() 上运行 API 调用而不是 breed被排除在回复之外我得到了以下回复。
{
"$__": {
"strictMode": true,
"selected": {},
"getters": {},
"_id": {
"_bsontype": "ObjectID",
"id": {
"type": "Buffer",
"data": [
94,
93,
76,
66,
116,
204,
248,
112,
147,
216,
167,
205
]
}
},
"wasPopulated": false,
"activePaths": {
"paths": {
"_id": "init",
"name": "init",
"age": "init",
"breed": "init",
"__v": "init"
},
"states": {
"ignore": {},
"default": {},
"init": {
"_id": true,
"name": true,
"age": true,
"breed": true,
"__v": true
},
"modify": {},
"require": {}
},
"stateNames": [
"require",
"modify",
"init",
"default",
"ignore"
]
},
"pathsToScopes": {},
"cachedRequired": {},
"$setCalled": [],
"emitter": {
"_events": {},
"_eventsCount": 0,
"_maxListeners": 0
},
"$options": {
"skipId": true,
"isNew": false,
"willInit": true
}
},
"isNew": false,
"_doc": {
"_id": {
"_bsontype": "ObjectID",
"id": {
"type": "Buffer",
"data": [
94,
93,
76,
66,
116,
204,
248,
112,
147,
216,
167,
205
]
}
},
"name": "Sylver",
"age": 14,
"breed": "Persian Cat",
"__v": 0
},
"$locals": {},
"$op": null,
"$init": true
}

任何人都可以帮助我如何正确序列化响应?

最佳答案

更新:
class-transformer 现在可以与 typegoose 一起正常工作,look here for the documentation on how to use it

this is an known issue ( #108 ), typegoose (& mongoose) 与类转换器/类验证器不兼容
这是因为 typegoose 需要将类转换为模式,而 mongoose 会将其编译为模型(不再是类)

关于serialization - 如何在使用 Typegoose 获取数据的同时使用类转换器序列化嵌套 js 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60525544/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com