03_트러블슈팅

김민창·2021년 11월 20일
0

trouble shooting

목록 보기
3/8
post-thumbnail

이슈

  • axiosparams를 담아 보내는 과정에서 배열로 보낼때 400에러와 함께 파라미터가 이상하게 담겨서 전달된다.

  • 코드
const dongCode=[1111017600,1111018000,1111018200...]
const params ={
    dong: dongCode,
};
dongList(
    params,
    (response) =>{
        commit("SET_DETAIL_HOUSE", response.data);
    },
    (error) => {
        console.log(error);
    }
);

// api는 axios 객체를 생성한 것
function dongList(params, success, fail) {
    api.get(`/map/apt`, { params: params }).then(success).catch(fail);
}
  • 400에러와 함께 발생하는 로그는 다음과 같다.

  • 하나의 배열로 묶여서 넘겨졌으면 했는데 파라미터 부분이 엉망진창이 되었다.



해결

  • params,로 구분하여 리스트형태로 전달하기 위해 join을 사용했다.
const params ={
    dong: dongCode.join(","),
};
  • 파라미터가 깔끔하게 전달되는것을 볼 수 있다.

참고사이트


이슈

리스트로 전달받은 파라미터를 활용하여 select문에 적용하니 리스트값이 아무것도 없을 때 mybatis내부 에러가 발생한다.

  • 코드
<select id="getAptInDong" resultType="houseInfoDto">

    select *
    from houseinfo
    where dongCode in
    <foreach collection="list" item="dong" open="(" separator="," close=")">
        #{dong}
    </foreach>

</select>
  • 에러와 함께 발생하는 로그는 다음과 같다.
    check the manual that corresponds to your MySQL server version for the right syntax to use near '~~~~'

  • MySQL 구문이 틀렸다고 하는건데 찾아보니 보통 where절을 따옴표로 묶지 않아서 생기는 문제라고 한다.



해결

  • 구문을 if로 조건을 나눠준다.

  • isEmpty() 함수를 통해 받아온 list가 비어있는지 확인해 준다.

<select id="getAptInDong" resultType="houseInfoDto">

    select *
    from houseinfo
    where dongCode in
    <if test="dong.isEmpty()">("")</if>
    <if test="!dong.isEmpty()">
    <foreach collection="list" item="dong" open="(" separator="," close=")">
        #{dong}
    </if>
    </foreach>

</select>
profile
개발자 팡이

0개의 댓글