Skip to content

fix: When I use UploadedFile I always get Can't set headers after they are sent. #892

Open
@air2

Description

@air2

Description

I use a UploadedFile decorator and multer to store the file on disk. But I always get the error:

Error: Can't set headers after they are sent.
    at SendStream.headersAlreadySent (C:\myproject\node_modules\send\index.js:386:13)
    at SendStream.send (C:\myproject\node_modules\send\index.js:613:10)
    at onstat (C:\myproject\node_modules\send\index.js:725:10)
    at FSReqCallback.oncomplete (fs.js:192:5)
    at FSReqCallback.callbackTrampoline (internal/async_hooks.js:130:17)

The response is received on the client with the correct response. So how do I find out what is causing this headers to be send?
I do not have a custom error handler, but still tried tried to disable the default error handler, it did not help.
All other endpoints work fine, only this upload handler gives me this error.

Minimal code-snippet showcasing the problem

export const fileUploadOptions: Options = {
  storage: diskStorage({
    destination: (_req: Express.Request, _file: Express.Multer.File, callback: (error: Error | null, destination: string) => void): void => {
      callback(null, join(getSettings().getTemporaryDirectory))
    },
    filename: (_req: Express.Request, _file: Express.Multer.File, callback: (error: Error | null, filename: string) => void): void => {
      callback(null, `${dayjs().format('YYMMDDHHmmss')}_${nanoid()}.dat`)
    }
  })
}

@Authorized([GroupType.Workloads, GroupType.Administrators])
@Post('/upload/')
upload (@UploadedFile('file', { options: fileUploadOptions }) file: Express.Multer.File): { result: string } {
return {
      result: file.filename
    }
}

Expected behavior

No error

Actual behavior

Seeing the following error:

Error: Can't set headers after they are sent.
    at SendStream.headersAlreadySent (C:\myproject\node_modules\send\index.js:386:13)
    at SendStream.send (C:\myproject\node_modules\send\index.js:613:10)
    at onstat (C:\myproject\node_modules\send\index.js:725:10)
    at FSReqCallback.oncomplete (fs.js:192:5)
    at FSReqCallback.callbackTrampoline (internal/async_hooks.js:130:17)

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs triageIssues which needs to be reproduced to be verified report.type: fixIssues describing a broken feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions