您可以将FileInterceptor与TransformPipe一起使用吗

时间:2018-11-16 11:49:20

标签: javascript node.js typescript nestjs

我遇到了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;
  }
}

1 个答案:

答案 0 :(得分:0)

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

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

@Body()

如果您使用@UsePipes(SamplePipe) @UseInterceptors(FileInterceptor('file')) @Post() samplePost(@UploadedFile() file, @Body() body) { ^^^^^^^^^^^^^ return file } ,该管道将在所有适用的地方应用。您也可以使用@UsePipes()仅将管道应用于主体。

相关问题