typestack / class-transformer

Decorator-based transformation, serialization, and deserialization between objects and classes.
MIT License
6.82k stars 500 forks source link

fix: Can't use instanceToPlain with Nitro.js event. #1635

Closed imcm7 closed 11 months ago

imcm7 commented 11 months ago

Description

Can't use instanceToPlain with Nitro event. https://github.com/unjs/nitro

import { defineEventHandler, H3Event } from 'h3';

export default defineEventHandler(async (event: H3Event): Promise<void> => {
  object = instanceToPlain(event);
}

Expected behavior

Plain object

Actual behavior

TypeError: Cannot read properties of undefined (reading 'emit')

Example of nitro project

https://github.com/imcm7/nitro-db/blob/main/routes/api/index.ts

imcm7 commented 11 months ago

Can't work with mikroorm query result, I try all functions const user = this.userRepository.findOne(1);

 instanceToPlain(user)
message":"Maximum call stack size exceeded","stack":"<pre><span class=\"stack internal\">at TransformOperationExecutor.getKeys (./node_modules/src/TransformOperationExecutor.ts:429:18

Not work with fields of type Collection

User {
  id: 1,
  roles: Collection<Role> { initialized: false, dirty: false },
}

and

User {
  id: 1,
  roles: Collection<Role> {
    '0': Role {
        active: true,
    }
}
imcm7 commented 11 months ago

It is because user has role, and role has user? But with instanceToPlain(x, { enableCircularCheck: true }), Class constructor ObjectHydrator cannot be invoked without 'new'

imcm7 commented 11 months ago

Work with instanceToPlain(x, { enableCircularCheck: true, excludePrefixes: ['_', '__'] })

github-actions[bot] commented 10 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.