【问题标题】:How to transform api response in nestjs?如何在nestjs中转换api响应?
【发布时间】:2019-11-20 05:47:45
【问题描述】:

我正在使用Nest Serialization 来转换 api 响应。但是,它返回的结果非常神秘。我希望它能从数据中切出一些字段,但由于某种原因,它显示出奇怪的结果。

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

@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

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))
}

返回,

   "$__": {
        "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
        }
    },

我在这里做错了什么还是转换 API 响应的方法不正确?

【问题讨论】:

    标签: javascript mongodb nestjs


    【解决方案1】:

    我也明白了。答案已在您的控制器中删除 @UseInterceptors(ClassSerializerInterceptor)

    也许该函数利用类扩展的对象继承?我认为您并不需要它,因为该函数用于转换响应(例如将 camerCase 转换为 snake_case 等)。如果你真的需要它,那我不知道,因为我也在努力。

    【讨论】:

      【解决方案2】:

      不确定NestJS,但在常规node.js 中为mongodb,如果您以这种方式调用find() X().X().X(),您需要在管道末尾添加对exec() 方法的调用,喜欢:

         this.productModel
                  .find()
                  .skip(from)
                  .limit(Number(per_page))
                  .exec();
      

      【讨论】:

        猜你喜欢
        • 2019-10-06
        • 2021-12-14
        • 2022-01-06
        • 2020-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-21
        • 2021-09-14
        相关资源
        最近更新 更多