TypeError: res.json is not a function

·2024년 5월 7일

stop_bang_review           | /usr/src/app/controllers/reviewController.js:47
stop_bang_review           |       return res.json(result);
stop_bang_review           |                  ^
stop_bang_review           | 
stop_bang_review           | TypeError: res.json is not a function
stop_bang_review           |     at /usr/src/app/controllers/reviewController.js:47:18
stop_bang_review           |     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
stop_bang_review           | 
stop_bang_review           | Node.js v21.7.3
stop_bang_main             | node:_http_client:524
stop_bang_main             |     req.emit('error', new ConnResetException('socket hang up'));
stop_bang_main             |                       ^
stop_bang_main             | 
stop_bang_main             | Error: socket hang up
stop_bang_main             |     at Socket.socketOnEnd (node:_http_client:524:23)
stop_bang_main             |     at Socket.emit (node:events:531:35)
stop_bang_main             |     at endReadableNT (node:internal/streams/readable:1696:12)
stop_bang_main             |     at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
stop_bang_main             |   code: 'ECONNRESET'
stop_bang_main             | }

마이크로서비스 통신을 하던 중 res.json이 function이 아니라는 오류가 발생했다.
코드는 아래와 같다.



module.exports = {

  // 이 후기가 7번 이상 신고된 후기인지 조회
  reportCheck: async (req, res) => {
    let resultInt = 0;
    
    const getOptions = {
      host: 'stop_bang_sub_DB',
      port: process.env.PORT,
      path: `/db/report/findAllByID/${req.params.rv_id}`,
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      }
    }

    httpRequest(getOptions)
    .then(res => {
      const repoCount =res.body.length;
    
      if(repoCount >= 7) resultInt = 0;
      else resultInt = 1;

      const result = {
        rv_id: req.params_rv_id,
        result: resultInt
      };
      
      return res.json(result);
      
    })
    
       
     /** 
     * result = {"rv_id", "result_int"}
     * result int 가 0이면 7번 이상, 1이면 7번 미만
     */
      
  	},
  }

해결

reportCheck의 res파라미터와 httpRequest.then 의 res가 중복되어 overwrite됐기 때문에 발생한 문제였다. then의 파라미터를 response로 변경하니 해결되었다.

reportCheck: async (req, res) => {
...
httpRequest(getOptions)
    .then(response => { ... } // 이 부분을 res에서 response로 변경
profile
🔥

0개의 댓글