• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

댓글질문있습니다

20.04.21 15:31 작성 조회수 128

0

댓글삭제 기능도 해보고싶어서 사가 리듀서 백라우트 프론트 코드 작성해봤어요~

401Unauthorized 에러뜨는데 어떤 부분을 고쳐야 할까요?

// 댓글삭제하기
    const onRemoveComments = useCallback(userId => () => {
        alert('댓글을 삭제하시겠습니까?');
        dispatch({
          type: REMOVE_COMMENT_REQUEST,
          data: userId,
        });
   });

< post.js/reducers >

 // 댓글 지우기
        case REMOVE_COMMENT_REQUEST: {
            return {
                ...state,
            };
        }
        case REMOVE_COMMENT_SUCCESS: { 
            const postIndex = state.mainPosts.findIndex(v => v.id === action.data.postId);
            const post = state.mainPosts[postIndex];
            const Comments = post.Comments.filter(v => v.id !== action.data.comments );
            const mainPosts = [...state.mainPosts];
            mainPosts[postIndex] = { ...postComments };
            return {
                ...state,
                mainPosts,
            };
        } 
        case REMOVE_COMMENT_FAILURE: {
            return {
                ...state,
            };
        }

< post.js/sagas >

// 댓글 지우기
function removeCommentsAPI(postId) {
  return axios.delete(`/post/${postId}/comment`);
}  
function* removeComments(action) {
  try {
      const result = yield call(removeCommentsAPIaction.data);
      yield put({
      type: REMOVE_COMMENT_SUCCESS,
      data: {
          postId: action.data,
          comments: result.data,
      },
      });
  } catch (e) {
      console.error(e);
      yield put({
      type: REMOVE_COMMENT_FAILURE,
      error: e,
      });
  }
}

-백

<post.js/routes>

// 댓글삭제하기 routes
router.delete('/:id/comment'isLoggedInasync (reqresnext=> {
  try {
    const comment = await db.Comment.findOne({ where: { id: req.params.id } });
    if (!comment) {
      return res.status(404).send('댓글이 존재하지 않습니다.');
    }
    await db.Comment.destroy({ where: { id: req.params.id } });
    res.send(req.params.id);
  } catch (e) {
    console.error(e);
    next(e);
  }
});

답변 2

·

답변을 작성해보세요.

0

401은 인증받지 않았다는 것입니다. 즉 로그인되지 않았다는 것이고요 프론트애서 백으로 쿠기가 전달 안된 겁니다. axios에서 withCrdentials 옵션 빠뜨리셨습니다.

0

지니님의 프로필

지니

질문자

2020.04.21

버튼 만들어서 해주었어요

<button type="button" className="remove" onClick={onRemoveComments(post.id)} >
             REMOVE
</button>