07-21-2023, 05:01 PM
In addition to [this answer](
How to use class-transformer groups in a dynamic way since the groups are fixed in the function decorator `@SerializeOptions`. Put the dynamic groups in `classToClass` or `plainToClass`.
```
@Post()
@SerializeOptions({
groups: ['admin','user'],
})
async create(
@Body() createProjectDto: CreateProjectDto,
) {
const data = await this.projectsService.create(createProjectDto);
return classToClass(data, { groups: ['admin','DYNAMIC_GROUP'] });
//Or you can return
//return plainToClass(Project, plainObject, {groups: ['admin','DYNAMIC_GROUP']});
}
```
The `classToClass` and `plainToClass` inside the function body do the actual filtering while `@SerializeOptions` to make sure the filtered data will be returned correctly.
[To see links please register here]
) for Nestjs.How to use class-transformer groups in a dynamic way since the groups are fixed in the function decorator `@SerializeOptions`. Put the dynamic groups in `classToClass` or `plainToClass`.
```
@Post()
@SerializeOptions({
groups: ['admin','user'],
})
async create(
@Body() createProjectDto: CreateProjectDto,
) {
const data = await this.projectsService.create(createProjectDto);
return classToClass(data, { groups: ['admin','DYNAMIC_GROUP'] });
//Or you can return
//return plainToClass(Project, plainObject, {groups: ['admin','DYNAMIC_GROUP']});
}
```
The `classToClass` and `plainToClass` inside the function body do the actual filtering while `@SerializeOptions` to make sure the filtered data will be returned correctly.