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

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

Open
air2 opened this issue Jun 10, 2022 · 1 comment
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.

Comments

@air2
Copy link

air2 commented Jun 10, 2022

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)
@air2 air2 added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels Jun 10, 2022
@thexpert507
Copy link

I have the same error, I have not been able to solve it for get requests,
But as for the post and put, the @Body({ transform: undefined }) fixed it momentarily.
I have noticed that it fails with certain object properties

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.
Development

No branches or pull requests

2 participants