routing-controllers
routing-controllers copied to clipboard
fix: When I use UploadedFile I always get Can't set headers after they are sent.
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)
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