나의 개발일지(김지헌)
항해 99 34일차 10/22일 본문
본격적으로 api를 구성하기로 하고 api설계서를 짜고 나서 설계에 들어갔다.
게시글 작성 | POST | /form | { title: "게시글 제목입니다.", content: "content 입니다." imageUrl:”” } |
설계를 완료 하고 코드를 짯었는데 오류가 떳는데
deletePost = async (req, res, next) => {
const { postId } = req.params
const { userId } = res.locals.user
await this.postsService.deletePost(postId, userId)
res.status(201).json({ "message": "게시글 삭제에 성공하였습니다" });
}
컨트롤러에서 서비스로 postId와 userId를 보내주는데 const { userId } = res.locals.user 에서 보낸 userId값이
서비스에서 문자로? 뜨는것이였다.
deletePost = async (postId, userId) => {
const findOnePost = await this.postsRepository.getfindById(postId)
if(findOnePost.userId !== userId){
throw new Error("작성자가 일치 하지 않습니다.")
}
return await this.postsRepository.deletePost(postId,userId)
}
그래서 구현한 api가 제대로 동작하지 않고 if문에서 걸려서 에러가 뜨는 것이였다.
해결 방법은 컨트롤러에서 서비스로 보낼때 중괄호로 묶어 주는것이였다.
//컨트롤러
deletePost = async (req, res, next) => {
const { postId } = req.params
const { userId } = res.locals.user
await this.postsService.deletePost({postId, userId})
res.status(201).json({ "message": "게시글 삭제에 성공하였습니다" });
}
//서비스
deletePost = async ({postId, userId}) => {
const findOnePost = await this.postsRepository.getfindById(postId)
if(findOnePost.userId !== userId){
throw new Error("작성자가 일치 하지 않습니다.")
}
return await this.postsRepository.deletePost({postId,userId})
}
'항해 99' 카테고리의 다른 글
항해 99 36일차 10/24일 (0) | 2022.10.25 |
---|---|
항해 99 35일차 10/23일 (0) | 2022.10.23 |
항해 99 33일차 10/21일 (0) | 2022.10.22 |
항해 99 32일차 10/20일 (0) | 2022.10.21 |
항해 99 31일차 10/19일 (0) | 2022.10.19 |