[英]How to transform api response in nestjs?
I am using Nest Serialization to transform api response.我正在使用Nest 序列化来转换 api 响应。 However, it returns really crypic result.
但是,它返回的结果非常糟糕。 I expect it to slice some fields out of the data but for some reason, it shows weird result.
我希望它从数据中切出一些字段,但由于某种原因,它显示出奇怪的结果。
product.entity.js product.entity.js
export class ProductEntity {
id: string
description: string
status: boolean
price: string
moq: number
rating: number
last_modified: string
constructor(partial: Partial<ProductEntity>) {
Object.assign(this, partial)
}
}
product.controller.ts product.controller.ts
@UseInterceptors(ClassSerializerInterceptor)
@Get()
async findAll(
@Query('page') page: number,
@Query('per_page') per_page: number
): Promise<ProductEntity[]> {
if (!page && !per_page) {
throw new BadRequestException('Proper query params not supplied')
}
const data: ProductEntity[] = []
const products: any = await this.productService.findAll({
page,
per_page
})
products.map(product => {
data.push(new ProductEntity(product))
})
return data
}
product.service.ts产品.服务.ts
async findAll(query): Promise<Product[]> {
const { page, per_page }: { page: number; per_page: number } = query
const from: number = (page - 1) * per_page
return this.productModel
.find()
.skip(from)
.limit(Number(per_page))
}
which returns,返回,
"$__": {
"strictMode": true,
"selected": {},
"getters": {},
"_id": {
"_bsontype": "ObjectID",
"id": {
"type": "Buffer",
"data": [
93,
15,
149,
140,
78,
148,
77,
10,
10,
105,
51,
130
]
}
},
"wasPopulated": false,
"activePaths": {
"paths": {
"moq_type": "init",
"moq": "init",
"price_type": "init",
"price": "init",
"name": "init",
"category_id": "init",
"company_id": "init",
"short_description": "init",
"long_description": "init",
"active": "init",
"variations": "init",
"keywords": "init",
"photos": "init",
"_id": "init",
"created_at": "init",
"updated_at": "init",
"__v": "init"
},
"states": {
"ignore": {},
"default": {},
"init": {
"_id": true,
"short_description": true,
"long_description": true,
"active": true,
"variations": true,
"keywords": true,
"photos": true,
"name": true,
"price": true,
"price_type": true,
"moq": true,
"moq_type": true,
"category_id": true,
"company_id": true,
"created_at": true,
"updated_at": true,
"__v": true
},
"modify": {},
"require": {}
},
"stateNames": [
"require",
"modify",
"init",
"default",
"ignore"
]
},
"pathsToScopes": {},
"cachedRequired": {},
"session": null,
"$setCalled": [],
"emitter": {
"_events": {},
"_eventsCount": 0,
"_maxListeners": 0
},
"$options": {
"skipId": true,
"isNew": false,
"willInit": true
}
},
Am I doing something wrong in here or is it not correct way to transform API response?我在这里做错了什么还是转换 API 响应的方法不正确?
Not sure about NestJS
but in regular node.js
for mongodb
, If you are calling find()
that way X().X().X()
you need to add at the end of the pipeline the call to exec()
method, like :不确定
NestJS
但在mongodb
常规node.js
中,如果您以这种方式调用find()
X().X().X()
您需要在管道的末尾添加对exec()
方法的调用, 喜欢 :
this.productModel
.find()
.skip(from)
.limit(Number(per_page))
.exec();
I am getting that too.我也明白了。 The answer is removed
@UseInterceptors(ClassSerializerInterceptor)
in your controller.答案已在控制器中删除
@UseInterceptors(ClassSerializerInterceptor)
。
Maybe that function utilize object inheritance from class extension?也许该函数利用类扩展的对象继承? I think you don't really need it because that function is used to cast a response (like tranforming
camerCase
to snake_case
, etc).我觉得你并不真的需要它,因为该功能是用于铸造的响应(如tranforming
camerCase
到snake_case
等)。 If you really need it, then I don't know because I am still strugle with it too.如果你真的需要它,那么我不知道,因为我也在与它斗争。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.