axios post 시 하나의 데이터만 보낼 때

chaaerim·2022년 7월 22일
0

백과 연결하기 시작하니 진짜 에러와의 전쟁... 중...

export const postScrapStateThunk = createAsyncThunk(
  'scrap/postScrapState',
  async (post: number, thunkAPI) => {
    const response = await axios.post('/feed/scrap/', { post });
    return response.data;
  }
);

export const deleteScrapStateThunk = createAsyncThunk(
  'scrap/deleteScrapState',
  async (post: number, thunkAPI) => {
    const response = await axios.delete('/feed/scrap/', {
      data: { post: post },
    });
    return response.data;
  }
);

발생 에러

원래 scrap post, delete 요청시 user와 postid를 한 객체에 담아 같이 보내주었는데 백에서 user 정보를 쿠키에서 가져와서 쓸 것 같다고 postid 데이터만 보내달라고 했다. 원래 문제없이 작동하던 코드라 오케이!! 하고 user를 post와 delete에서 삭제하는 순간 Axios error 파티를 마주했다. 백에서 post 데이터가 int형인데 string으로 읽혀서 이러한 에러가 발생한 것 같다.

해결 방법

백에서 post를 받을 때 데이터를 object에서 꺼내고 프론트에서도 postid를 객체로 보내주니 해결되었다..
진짜 끝이 없다 어휴

0개의 댓글