我遇到了TransformPipe的问题-只要不使用FileInterceptor,它就可以正常工作。由于我需要这两种功能,所以让我感到困惑。我在Github上创建了issue,但Kamil对此进行了写道,这是正常的框架行为。我,我的朋友们都没有在官方文档中找到对此“正常”行为的任何引用。你有什么想法?

代码为here

控制者

@UsePipes(SamplePipe)
@UseInterceptors(FileInterceptor('file'))
@Post()
samplePost(@UploadedFile() file) {
  return file
}




@Injectable()
export class SamplePipe implements PipeTransform {
  transform(value: any, metadata: ArgumentMetadata) {
    console.log("I'm working")
    return value;
  }
}

最佳答案

Pipes仅作为以下类型起作用:'body' | 'query' | 'param' | 'custom'对应于@Body()@Query()@Param()custom decorators,例如@User()。在您的示例中,您没有任何这些,这就是为什么不应用管道的原因。

因此,如果将其中之一添加到示例中,则将应用管道(在本例中为@Body())。

@UsePipes(SamplePipe)
@UseInterceptors(FileInterceptor('file'))
@Post()
samplePost(@UploadedFile() file, @Body() body) {
                                 ^^^^^^^^^^^^^
  return file
}


如果使用@UsePipes(),则该管道将在所有适用的地方应用。您也可以使用@Body(SimplePipe) body仅将管道应用到主体。

09-25 17:10
查看更多