강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

김건희님의 프로필 이미지
김건희

작성한 질문수

[리뉴얼] React로 NodeBird SNS 만들기

질문있습니다.

해결된 질문

작성

·

245

0

질문1.

commentForm.js에서 댓글을 작성하고 버튼을 누르면 밑의 코드가 실행됩니다. 

const onSubmitComment = useCallback(() => {
    dispatch({
      type: ADD_COMMENT_REQUEST,
      data: { content: commentTextpostId: post.iduserId: id },
    });
  }, [commentTextid]);

여기서 인자로 userId를 보내주었는데 리듀서와 사가를 거쳐 백엔드에서

router.post('/:postId/comment'isLoggedInasync (reqresnext=> { // POST /post/1/comment
  try {
    const post = await Post.findOne({
      where: { id: req.params.postId },
    });
    if (!post) {
      return res.status(403).send('존재하지 않는 게시글입니다.');
    }
    const comment = await Comment.create({
      content: req.body.content,
      PostId: parseInt(req.params.postId10),
      UserId: req.user.id,
    })
    const fullComment = await Comment.findOne({
      where: { id: comment.id },
      include: [{
        model: User,
        attributes: ['id''nickname'],
      }],
    })
    //  console.log("fullComment::::",JSON.stringify(fullComment),"enddddd");
    res.status(201).json(fullComment);
  } catch (error) {
    console.error(error);
    next(error);
  }
});

위의 라우터가 실행되잖아요? 그런데 여기서 인자로 보내준 userId는 안쓰이고 req.user.id즉 세션에 저장되어 있는것을 쓰셨는데  그렇다면 위에서 userId를 인자로 넘겨준 이유가 무엇인지 궁금합니다.

질문2. 

FollowButton.js입니다 

팔로우 버튼을 누르면

const FollowButton = ({ post }) => {
  const dispatch = useDispatch();
  const { mefollowLoadingunfollowLoading } = useSelector((state=> state.user);
  const isFollowing = me?.Followings.find((v=> v.id === post.User.id);
 console.log(isFollowing);
  const onClickButton = useCallback(() => {
    if (isFollowing) {
      dispatch({
        type: UNFOLLOW_REQUEST,
        data: post.User.id,
      });
    } else {
      dispatch({
        type: FOLLOW_REQUEST,
        data: post.User.id,
      });
    }
  }, [isFollowing]);

  if (post.User.id === me.id) {
    return null;
  }

  return (
    <Button loading={followLoading || unfollowLoading} onClick={onClickButton}>
      {isFollowing ? '언팔로우' : '팔로우'}
    </Button>
  );
};

위의 코드가 실행되잖아요? 그런데 이때 console.log(isFollowing);으로 로그를 찍어 보았습니다.

그런데 팔로우 버튼을 누르면 console에서 

  1. {id: 9}
  2. id9

이렇게 아이디만 뜨고 다시 새로고침을 해주어야

  1. {id: 9, Follow: {…}}
    1. id9

아이디와 아이디에 속해있는 Follow 객체를 불러오는데요... 왜 팔로우 버튼을 누른 후에는 (새로고침 하기전에는 )

id만 받아 오는것인지 궁금합니다!

답변 1

0

제로초(조현영)님의 프로필 이미지
제로초(조현영)
지식공유자

1. 안 쓰이는 것이라 안 넘겨줘도 됩니다.

2. FOLLOW_SUCCESS 리듀서를 보시면 아실 수 있습니다.

김건희님의 프로필 이미지
김건희

작성한 질문수

질문하기