Class-transformer: fix: TypeError: Promise resolver undefined is not a function

Created on 24 May 2019  路  8Comments  路  Source: typestack/class-transformer

Not sure if this is an error, or expected behaviour.

import { plainToClass } from 'class-transformer'

class Foo {
  public num: number

  public str: Promise<string>
}

describe('Foo', () => {
  it('should turn to object', () => {
    const res = plainToClass(Foo, {
      num: 1,
      str: Promise.resolve('a'),
    })

    expect(res.num).toBe(1)
    expect(res.str).resolves.toBe('a')
  })
})

This fails with:

TypeError: Promise resolver undefined is not a function
    at new Promise (<anonymous>)

  at TransformOperationExecutor.Object.<anonymous>.TransformOperationExecutor.transform (src/TransformOperationExecutor.ts:123:32)
  at _loop_1 (src/TransformOperationExecutor.ts:246:43)
  at TransformOperationExecutor.Object.<anonymous>.TransformOperationExecutor.transform (node_modules/class-transformer/TransformOperationExecutor.js:260:17)
  at ClassTransformer.Object.<anonymous>.ClassTransformer.plainToClass (src/ClassTransformer.ts:43:25)
  at Object.plainToClass (src/index.ts:37:29)
  at Object.it (__tests__/extractors/transform.test.ts:11:17)

Any ideas why?

Thanks!

duplicate fix

Most helpful comment

Also running into this issue when using graphql-upload with NestJS and the ValidationPipe.

All 8 comments

Also running into this issue when using graphql-upload with NestJS and the ValidationPipe.

Also running into this issue when using graphql-upload with NestJS and the ValidationPipe.

Any solution?

@lookapanda Not really a solution but my workaround was to @Exclude() the upload field.

@PachowStudios thanks, this prevents the error - but if the field is excluded, it's also not available when using the class/dto.
Have you found any solution for this?

Unfortunately also @Transform-decorator is not usable as workaround for this issue as its executed after & additionally to default transform.

@jbbr We pass transform: false to ValidationPipe so you just get the raw DTO. This of course prevents you from doing any kind of transformation, however this has worked fined for us so far.

Another option could be to create your own ValidationPipe (which we also did, but for unrelated reasons), and have it handle transforming the Promise fields.

Side note: The reason we made our own ValidationPipe was because the stock NestJS one recursively strips prototype keys from the input. We use sentry.io which apparently tacks a context property on to Promise which is recursive and causes the strip prototype keys function to infinitely loop. As a result, we had to make our own custom pipe which doesn't have this behavior. Hopefully this helps anyone running into something similar.

Regardless, this issue needs to be fixed.

This has a PR at #262

A tracking issue has been created for this feature request: #549. Closing this as duplicate.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ERPedersen picture ERPedersen  路  4Comments

NathanHazout picture NathanHazout  路  5Comments

patricknazar picture patricknazar  路  4Comments

samfrach picture samfrach  路  6Comments

VivienAdnot picture VivienAdnot  路  6Comments