Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ParseFilePipeBuilder Cannot read properties of undefined (reading 'size') when upload file #10017

Closed
3 of 15 tasks
aribambang opened this issue Jul 25, 2022 · 8 comments
Closed
3 of 15 tasks
Labels
needs triage This issue has not been looked into

Comments

@aribambang
Copy link

aribambang commented Jul 25, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

i'm uploading image file using form-data, when i don't attach file then in ParseFilePipeBuilder function i get error ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'size')

[Nest] 56810  - 07/25/2022, 4:34:31 AM   ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'size')
TypeError: Cannot read properties of undefined (reading 'size')
    at MaxFileSizeValidator.isValid (/Users/aribambang/project/node_modules/@nestjs/common/pipes/file/max-file-size.validator.js:20:21)
    at ParseFilePipe.validateOrThrow (/Users/aribambang/project/node_modules/@nestjs/common/pipes/file/parse-file.pipe.js:39:41)
    at ParseFilePipe.validate (/Users/aribambang/project/node_modules/@nestjs/common/pipes/file/parse-file.pipe.js:34:24)
    at ParseFilePipe.transform (/Users/aribambang/project/node_modules/@nestjs/common/pipes/file/parse-file.pipe.js:28:24)
    at /Users/aribambang/project/node_modules/@nestjs/core/pipes/pipes-consumer.js:16:33
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at resolveParamValue (/Users/aribambang/project/node_modules/@nestjs/core/router/router-execution-context.js:147:23)
    at async Promise.all (index 0)
    at pipesFn (/Users/aribambang/project/node_modules/@nestjs/core/router/router-execution-context.js:150:13)
    at /Users/aribambang/project/node_modules/@nestjs/core/router/router-execution-context.js:37:30

image

@UseInterceptors(FileInterceptor('logo'))
  @Post()
  async create(
    @Body() createDto: CreateDto,
    @UploadedFile(
      new ParseFilePipeBuilder()
        .addMaxSizeValidator({
          maxSize: 1048576,
        })
        .addFileTypeValidator({
          fileType: /\.(jpe?g|png)$/i,
        })
        .build({
          errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
        }),
    )
    logo?: Express.Multer.File,
  ): Promise<void> {
    console.log(createDto);
    console.log(logo);
  }

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-amuib7

Steps to reproduce

No response

Expected behavior

We must to create a validation function on ParseFilePipeBuilder when the file doesn't exist during upload.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

9.0.5

Packages versions

    "@nestjs-modules/mailer": "^1.8.1",
    "@nestjs/common": "^9.0.5",
    "@nestjs/config": "^2.2.0",
    "@nestjs/core": "^9.0.5",
    "@nestjs/jwt": "^9.0.0",
    "@nestjs/passport": "^9.0.0",
    "@nestjs/platform-express": "^9.0.5",
    "@nestjs/sequelize": "^9.0.0",

Node.js version

No response

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@aribambang aribambang added the needs triage This issue has not been looked into label Jul 25, 2022
@jmcdo29
Copy link
Member

jmcdo29 commented Jul 25, 2022

You know, linking to our own code isn't a minimum reproduction.

I'm surprised this happens rather than multer throwing an exception as interceptors are triggered before pipes.

Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project).

@aribambang
Copy link
Author

You know, linking to our own code isn't a minimum reproduction.

I'm surprised this happens rather than multer throwing an exception as interceptors are triggered before pipes.

Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project).

i'm sorry, I have updated the minimum reproduction.

@jmcdo29
Copy link
Member

jmcdo29 commented Jul 25, 2022

Okay, I can confirm that multer only cares about the field existing, not necessarily that there is a file to parse there. We should probably add a FileExistsValidator that we prepend to the array if there are validators to be ran

@kamilmysliwiec
Copy link
Member

cc @thiagomini

@thiagomini
Copy link
Contributor

I'll have a look on that and work in a fix asap, thanks for reporting!

@thiagomini
Copy link
Contributor

thiagomini commented Jul 26, 2022

@kamilmysliwiec Created a PR with the fix for this issue, would appreciate your review on that

@woutrbe
Copy link

woutrbe commented Jul 27, 2022

I'm dealing with the same issue as this, where I have an optional file upload field. While PR seems like a good solution, is there any way to use the ParseFilePipeBuilder manually in the controller? So that I can catch the errors myself?

@Post('/')
@UseInterceptors(FileInterceptor('logo'))
public async save(
   @Body() body: SettingsDto,
   @UploadedFile() file: any,
   @Res() res: Response,
) {
   try {
      // How would I use ParseFilePipeBuilder here? 
     new ParseFilePipeBuilder()
         .addFileTypeValidator({
		fileType: 'ico'
	  })
         .addMaxSizeValidator({
		maxSize: 50000
         })
	 .build()
   } catch(err) { 
        return res.render('/views/settings.pug', { errror: err.message })
   }
}

@kamilmysliwiec
Copy link
Member

Let's track this here #10025

@nestjs nestjs locked and limited conversation to collaborators Jul 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

5 participants