3월 19일 TIL

임덤덤·2023년 3월 19일
0
  • 오늘은 카테고리 수정에 오류가 발생되서 해결해봤다
    • 사실 생성 자체가 잘못되서 수정에 오류가 생긴것
  • 이전에는 카테고리를 생성할때
const lastCategoryId = categoryList[categoryList.length - 1].categoryId + 1;

  const addCategoryHandler = (categoryId: number) => {
    if (categoryValue !== "전체") {
      setNewCategory(!newCategory);
      const newCategoryData = {
        categoryId: categoryId,
        categoryName: categoryValue,
      };
      categoryList.push(newCategoryData);
      setCategoryValue("");
    }
  };

  <button
    className="Category_Submit_Button"
    onClick={async () => {
      createCategory(categoryValue);
      addCategoryHandler(lastCategoryId);
    }}
  >
    확인
  </button>
  • 위 방법대로 생성하고 있었고 당연스럽게도 CategoryId가 정상적으로 들어가지 않았다
    • 이전 데이터는 user별로 카테고리 ID가 생겨서 1,2,3,4,5 이순서대로 진행되는걸 생각했지만 카테고리 자체가 누적값으로 변경되면서 혼선이 생겼던 부분

해결

  • 서버에 response Data로 Category에 대한 정보를 넘겨달라고 했음
    • 그 이후 addCategoryHandler의 매개변수로 넣어줌
  <button
    className="Category_Submit_Button"
    onClick={async () => {
      const categoryId = await createCategory(categoryValue);
      addCategoryHandler(categoryId as number);
    }}
  >
    확인
  </button>
  • 서버에서 넘겨준 categoryId로 생성되니 로직 정상작동 확인
profile
응애🐣 예비 개발자 입니다.

0개의 댓글