Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client 에러 해결

2022. 12. 6. 19:26문제 해결

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client 에러 해결

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:393:5)
    at ServerResponse.setHeader (node:_http_outgoing:607:11)
    at ServerResponse.header (/Users/yuyu/project-3/node_modules/express/lib/response.js:794:10)
    at ServerResponse.send (/Users/yuyu/project-3/node_modules/express/lib/response.js:174:12)
    at ServerResponse.json (/Users/yuyu/project-3/node_modules/express/lib/response.js:278:15)
    at ServerResponse.send (/Users/yuyu/project-3/node_modules/express/lib/response.js:162:21)
    at editProfileInfo (/Users/yuyu/project-3/controllers/profileControllers.js:36:10)
    at Layer.handle [as handle_request] (/Users/yuyu/project-3/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/yuyu/ASOproject-3/node_modules/express/lib/router/route.js:144:13)
    at multerMiddleware (/Users/yuyu/project-3/node_modules/multer/lib/make-middleware.js:13:41) {
  code: 'ERR_HTTP_HEADERS_SENT'
}

Node.js v18.9.0
[nodemon] app crashed - waiting for file changes before starting...

 

 

단순히 해결하기

이 곳에서 오류가 났다

if (req.files === undefined)
        res.status(200).send({ msg: "변경된 내용이 없습니다" });

이렇게 해주니까 해결되었다

if (req.files === undefined)
       return res.status(200).send({ msg: "변경된 내용이 없습니다" });

 

에러 코드 뜯어보기

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

 

발생하는 이유