【问题标题】:Remove underscores from a response从响应中删除下划线
【发布时间】:2022-02-11 08:15:23
【问题描述】:

这是控制器:

@Controller('products')
@UsePipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true }))
export class ProductController {
    constructor(
        private readonly productService: ProductService,
    ) {}
    
    @Get()
    public async all(): Promise<ProductDto[]> {
        return this.productService.all();
    }

    @Post()
    @ApiResponse({type: ProductDto})
    public async create(@Body() createProductDto: CreateProductDto): Promise<ProductDto> {
        return this.productService.create(createProductDto);
    }
}

这里是服务:

@Injectable()
export class ProductService {
    constructor(
        @InjectRepository(ProductEntity)
        private readonly productRepository: Repository<ProductEntity>,
    ) {}

    public async all(): Promise<ProductDto[]> {
        return this.productRepository.find().then(products => products.map(p => new ProductDto().deserialize(p)));
    }

    public async create(createProductDto: CreateProductDto): Promise<ProductDto> {
        const productEntity = new ProductEntity();
        productEntity.image = createProductDto.image;
        productEntity.title = createProductDto.title;
        return productEntity.save().then(pe => new ProductDto().deserialize(pe));
    }
}

这是 ProductDto:

export class ProductDto implements Readonly<ProductDto>, IDeserializable<ProductDto, ProductEntity> {
    @ApiProperty({name: 'id'})
    @IsNumber()
    private _id: number;

    @ApiProperty({name: 'likes'})
    @IsNumber()
    private _likes: number;

    @ApiProperty({name: 'title'})
    @IsString()
    private _title: string;

    @ApiProperty({name: 'image'})
    @IsString()
    private _image: string;

    public set id(val: number) {
        this._id = val;
    }

    public get id(): number {
        return this._id;
    }

    public set title(val: string) {
        this._title = val;
    }

    public get title(): string {
        return this._title;
    }

    public set image(val: string) {
        this._image = val;
    }

    public get image(): string {
        return this._image;
    }

    public set likes(val: number) {
        this._likes = val;
    }

    public get likes(): number {
        return this._likes;
    }

    public deserialize(data: ProductEntity): ProductDto {
        this.image = data.image;
        this.title = data.title;
        this.likes = data.likes;
        this.id = data.id;

        return this;
    }
}

响应中包含下划线,我忘记如何删除了。

当我创建一个新产品时,我得到这个:

{
    "_image": "https://www.images.com/763267382.jpg",
    "_title": "porsche",
    "_likes": 0,
    "_id": 26
}

但我想要这个:

{
    "image": "https://www.images.com/763267382.jpg",
    "title": "porsche",
    "likes": 0,
    "id": 26
}

我该怎么做?

编辑:

我可以编写一个自定义管道来修剪它们,但除非我非常错误地有一个内置修剪或其他东西,我不记得了。

【问题讨论】:

    标签: typescript nestjs


    【解决方案1】:

    您可以通过将@UseInterceptors(ClassSerializerInterceptor)添加到控制器来启用ClassSerializerInterceptor,然后使用class-transformer包中的@Expose装饰器在序列化过程中更改名称。

    这是它的样子:

    @Controller('products')
    @UseInterceptors(ClassSerializerInterceptor)
    @UsePipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true }))
    export class ProductController {
    ...
    }
    

    还有ProductDto:

    import { Expose } from 'class-transformer';
    
    export class ProductDto implements Readonly<ProductDto>, IDeserializable<ProductDto, ProductEntity> {
        @Expose({ name: 'id' })
        @ApiProperty({name: 'id'})
        @IsNumber()
        private _id: number;
    
        @Expose({ name: 'likes' })
        @ApiProperty({name: 'likes'})
        @IsNumber()
        private _likes: number;
    
        @Expose({ name: 'title' })
        @ApiProperty({name: 'title'})
        @IsString()
        private _title: string;
    
        @Expose({ name: 'image' })
        @ApiProperty({name: 'image'})
        @IsString()
        private _image: string;
    
        ...
    

    您可以找到更多信息here

    【讨论】:

    • 是的,它就像一个魅力。谢谢!
    猜你喜欢
    • 2019-11-25
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 2018-08-24
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多