Skip to content

fix: Class constructor Model cannot be invoked without 'new' #1021

@jdpt0

Description

@jdpt0

Oops, this was meant for the NestJS repo, but is probably useful here too.

Description

nestjs/nest#7281 and this commit were intended to open up what could be transformed by the class-serializer interceptor, which was released with version 8.0.0 of NestJS. I've been trying to upgrade from version 7.16.0 to 8.2.3 but I'm running into issues with class-transformer, which is needed as we use Sequelize. I'm getting errors that seem similar to #242 and #596 using both class-transformer version 0.4.1 and 0.5.1.

Minimal code-snippet showcasing the problem
I've attempted to create a minimal reproduction, but I'm so far unsuccessful. However, if I add the line removed above back in, everything works without a problem. I'll keep attempting to figure out exactly what the problem is, but realistically, it's probably a problem with class-transformer rather than NestJS. Most of this low-level stuff goes over my head, so I'm not sure where to go from here, or if I'll actually be able to understand exactly why this is breaking.

EDIT: I found the cause of the issue. It's related to nested Sequelize objects. A minimal reproduction can be found at https://github.com/jdpowell1/class-transformer-bug

Expected behavior

When querying an endpoint that returns a Sequelize object, transform it correctly.

Actual behavior

This error is thrown.

TypeError: Class constructor Model cannot be invoked without 'new'
    at TransformOperationExecutor.transform (...\.yarn\cache\class-transformer-npm-0.5.1-96b5161e6c-f191c8b4cc.zip\node_modules\src\TransformOperationExecutor.ts:207:39)
    at ...\.yarn\cache\class-transformer-npm-0.5.1-96b5161e6c-f191c8b4cc.zip\node_modules\src\TransformOperationExecutor.ts:79:30
    at Array.forEach (<anonymous>)
    at TransformOperationExecutor.transform (...\.yarn\cache\class-transformer-npm-0.5.1-96b5161e6c-f191c8b4cc.zip\node_modules\src\TransformOperationExecutor.ts:44:24)
    at TransformOperationExecutor.transform (...\.yarn\cache\class-transformer-npm-0.5.1-96b5161e6c-f191c8b4cc.zip\node_modules\src\TransformOperationExecutor.ts:327:31)
    at TransformOperationExecutor.transform (...\.yarn\cache\class-transformer-npm-0.5.1-96b5161e6c-f191c8b4cc.zip\node_modules\src\TransformOperationExecutor.ts:327:31)
    at TransformOperationExecutor.transform (...\.yarn\cache\class-transformer-npm-0.5.1-96b5161e6c-f191c8b4cc.zip\node_modules\src\TransformOperationExecutor.ts:327:31)
    at ClassTransformer.instanceToPlain (...\.yarn\cache\class-transformer-npm-0.5.1-96b5161e6c-f191c8b4cc.zip\node_modules\src\ClassTransformer.ts:25:21)
    at Object.classToPlain (...\.yarn\cache\class-transformer-npm-0.5.1-96b5161e6c-f191c8b4cc.zip\node_modules\src\index.ts:23:27)
    at ClassSerializerInterceptor.transformToPlain (...\.yarn\__virtual__\@nestjs-common-virtual-63645f8124\0\cache\@nestjs-common-npm-8.2.3-f49d058617-2a06ffaf58.zip\node_modules\@nestjs\common\serializer\class-serializer.interceptor.js:47:32)

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidIssues with no action to taketype: fixIssues describing a broken feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions