I started to work in a new NestJs project but I'm facing an issue when I try to implement serialization. I want to implement serialization to transform objects before they gets sent in a network response. My project was working correctly but when I tried to implement ClassSerializerInterceptor in my controller I got the following error:
[Nest] 27010 - 12/23/2019, 8:20:53 PM [ExceptionsHandler] Maximum call stack size exceeded +29851ms
RangeError: Maximum call stack size exceeded
at Object.Console.<computed> (internal/console/constructor.js:241:9)
at Object.log (internal/console/constructor.js:282:26)
at Object.consoleCall (<anonymous>)
at _loop_1 (/path/to/my/project/node_modules/class-transformer/TransformOperationExecutor.js:146:47)
I changed the scope of ClassSerializerInterceptor to solve the problem but the error persists. According to the documentation, I need to use the interceptor in a controller and use the corresponding decorators in an entity to implement serialization. My implementation of serialization is the following:
billing-statement.controller.ts
import { ClassSerializerInterceptor, Controller, Get, Query, UseInterceptors } from '@nestjs/common';
import { BillingStatementService } from './billing-statement.service';
import { BillingStatementDto } from './billing-statement.dto';
import { BillingStatement } from './billing-statement.entity';
@Controller('billing-statement')
export class BillingStatementController {
constructor(private readonly billingStatementService: BillingStatementService) {}
@Get()
@UseInterceptors(ClassSerializerInterceptor)
async getBillingStatement(
@Query() query: BillingStatementDto,
): Promise<BillingStatement> {
return this.billingStatementService.findBillingStatementByUser(+query.id);
}
}
billing-statement.entity.ts
import { AutoIncrement, BelongsTo, Column, ForeignKey, HasMany, Model, PrimaryKey, Table } from 'sequelize-typescript';
import { User } from '../users/user.entity';
import { Payment } from './payment.entity';
import { Exclude } from 'class-transformer';
@Table({
tableName: 'billing_statement_tbl',
timestamps: false,
})
export class BillingStatement extends Model<BillingStatement> {
@AutoIncrement
@PrimaryKey
@Column({field: 'billing_statement_id_pk'})
id: number;
@Column
currency: string;
@Column({field: 'total_amount'})
totalAmount: number;
@Exclude()
@Column({field: 'contract_start'})
contractStart: Date;
@Exclude()
@Column({field: 'contract_end'})
contractEnd: Date;
@HasMany(() => Payment)
payments: Payment[];
}
I don't know what I'm doing wrong or what is the source of the error.