20241204 시놀인턴 8일차 오류수정

2024. 12. 4. 13:51TIL

어제부터 평소와 같이 코드를 짜고 테스트를 하는 데 오류가 발생했다.

예상하기론 라우터오류 같았다.

문제가 있는부분은 질문리스트 조회 관련 api 주소 호출 오류
질문 관련 주소는 /mbti/question 으로 시작해서 요청 종류 get, post, delete 등과 엔드포인트 를 이용해서 나뉘도록 해놨다.

모든 질문 조회는 계획상으론 GET /mbti/question/all 을 이용하려 했으나
요청 결과 오류는 나타나지 않았지만 반환받은 data가 null이였다.

 

어제부터 수많은 시도와 검색끝에 오늘 아침 고치는데에 성공했다.

 

결과부터말하자면 이유는 라우터 배치순서차이...

const express = require('express');
const router = express.Router();
const MbtiQuestionController = require('../../controller/mbti/mbtiQuestionController');
const url = "/mbti/question";


// 질문 생성
router.post('/', (req, res, next) => MbtiQuestionController.createQuestion(req, res, next));

// 모든 질문 조회
router.get('/all', (req, res, next) => MbtiQuestionController.getAllQuestions(req, res, next));

// 질문 id로 조회
router.get('/:id', (req, res, next) => MbtiQuestionController.getQuestion(req, res));

// 질문 수정
router.put('/:id', (req, res, next) => MbtiQuestionController.updateQuestion(req, res, next));

// 질문 삭제
router.delete('/:id', (req, res, next) => MbtiQuestionController.deleteQuestion(req, res, next));

module.exports = {
    url: url,
    router,
};

 

이는 현재 고쳐진 코드인데 원래는 모든질문조회가 get 중에 가장 아래에있었다.

문제는 라우터는 위에서부터 아래로 읽기 때문에 GET /.../all을 GET /.../:id 쪽에서 가져가버렸다는 것.
id쪽에 all이 들어가니 요청은 제대로 왔지만 반환해준 데이터가 null일 수밖에 없다.

 

결과적으로 순서만 바꾸니까 다시 정상작동하긴 했다.