NestJs ParseEnumPipe 无法解析

Mik*_*e M 3 javascript typescript nestjs nestjs-config

我正在使用 NestJs 框架(顺便说一句,很喜欢它),我想检查传入的数据,使其符合 Typscript 中的枚举。所以我有以下内容:

enum ProductAction {
  PURCHASE = 'PURCHASE',
}

@Patch('products/:uuid')
async patchProducts(
    @Param('uuid', ParseUUIDPipe) uuid: string,
    @Body('action', ParseEnumPipe) action: ProductAction,
  ) {

    switch(action) {

    ... code 
  }
Run Code Online (Sandbox Code Playgroud)

奇怪的是,当我运行这段代码时,第一个管道被编译

2022-07-21 16:53:51 [error] [ExceptionHandler] Nest can't resolve dependencies of the ParseEnumPipe (?, Object). Please make sure that the argument Object at index [0] is available in the FriendsModule context.
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

Jay*_*iel 11

您应该使用@Body('action', new ParseEnumPipe(ProductAction)) action: ProductAction枚举,因为枚举不会直接反映 Nest 读取其元数据的情况,并且因为 Nest 会尝试弄清楚如何Object在真正应该注入枚举时进行注入。