관리 메뉴

나의 개발일지(김지헌)

항해 99 23일차 10월 11일 본문

카테고리 없음

항해 99 23일차 10월 11일

코딩이좋아요 2022. 10. 12. 02:58

어제 부터 해서 몽구스를 사용 한 코드들을 MySql과 시퀄라이즈를 사용 하는 방법으로 바꾸면서 진행 했다.

바꿀때 마다 에러가 너무 많아서 하나하나 찾아보면서 진행을 했다.

수정 하는 코드인데 udateOne을 사용 하는 코드가 조건문을 더해져서 좀 바뀌어서 많이 햇갈렸다

더보기

router.put('/:commentId', authMiddleware,async (req,res)=>{    //일단 아이디 받아오고 일치하는지 확인 해당 
  try{        
      const user = res.locals.user // id 를 가져옴 2번
       const { commentId } = req.params 
       const {comment} = req.body   //수정할 코맨트
       const findUser = await Comments.findOne({ where :{commentId,}}) //아이디를 찾아옴  
       if(user.userId !== findUser.userId){ //예외 처리
        res.status(400).send({'message': "작성자와 일치 하지 않습니다."})
          return
       }        
        await Comments.update({ //put같은거
          comment : comment},
          {
          where : {commentId : commentId}
        });
        res.status(201).send({'message': "댓글을 수정했습니다"})            
      } catch(error){ //catch가 에러를 받는다.
        console.log(error)
        res.status(400).send({'message': "댓글 수정하기 error"}) 
      }   
})