首页 > 解决方案 > 您可以将 FileInterceptor 与 TransformPipe 一起使用吗

问题描述

我遇到了一个问题TransformPipe- 只要我不使用FileInterceptor. 由于我需要这两种功能,这让我很困惑。我在 Github 上创建了一个问题,但卡米尔在上面写道,这是一种正常的框架行为。我和我的朋友也没有在官方文档中找到任何对这种“正常”行为的引用。你有什么想法?

代码在这里

控制器

@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;
  }
}

标签: javascriptnode.jstypescriptnestjs

解决方案


Pipes仅适用于以下类型:'body' | 'query' | 'param' | 'custom'对应于@Body()、或自定义装饰器@Query(),如. 在您的示例中,您没有任何这些,这就是不应用管道的原因。@Param()@User()

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

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

如果您使用@UsePipes()管道,将适用于任何适用的地方。您也可以使用@Body(SimplePipe) body仅将管道应用于主体。


推荐阅读