• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

댓글기능 질문

20.06.12 13:16 작성 조회수 146

0

강사님 안녕하세요^^

댓글기능 관련 질문 드려요.

Comment.js 컴포넌트에서 '/api/comment/saveComment'에 variable을 보내는데,

variable은 다음과 같습니다.

const variable={
            writer:user.userData._id,
            postId:videoId,
            content:commentValue
       }

이제 comment.js route로 와서 다음을 실행합니다.

router.post('/saveComment', (reqres)=>{
    const comment = new Comment(req.body)
    comment.save((err,commentInfo)=>{
        if(errreturn res.status(400).send(err);
        
(★★★★★)Comment.find({'_id':comment._id})
            .populate('writer')
            .exec((err,result)=>{
                if(errreturn res.status(400).send(err)
                return res.status(200).json({success:true, result})
            })
    })
})

위의 5번째 줄 : Comment.find( {'_id' : comment._id} )

이 부분에서 어떻게 comment._id가 입력 가능한지 궁금합니다.

1) req.body에서는 userData의 _id를 'writer'라는 이름으로 axios 햇으니

'writer'로 받아야 하는것은 아닌지요?

2) const comment = new Comment(req.body)

이 부분 때문이라고 해도.. Comment 모델이나 User 모델에는 '_id'가 나와있지 않고,

'writer'만 나와있는데 어떻게 'comment._id'로 받아지는 건지 궁금합니다.

++++++++++++++++++++++++++++++++++)

그리고

  1) commentLists를 mapping한 것을 console창에 띄워보면, _id와 writer에 속한 _id가 각각 존재하던데

이 둘의 차이는 무엇인지 궁금합니다.

  2) 또 parentCommentId로 'comment._id' 값을 주셨는데, 이것은 writer의 _id인지, 그냥 _id 인지요??

writer의 _id 토큰값은 동일하고, 그냥 _id 토큰값은 매 댓글마다 변경되는 듯 합니다.

그럼 답변 부탁드립니다^^

답변 1

답변을 작성해보세요.

0

안녕하세요  ~ 

혹시 제가 강의 중에    commentInfo  이렇게  값을 받은 후에   comment._id  이런식으로 했었나요 ...?

만약 그렇게 했었다면 실수를 한것이며 

commentInfo  로 오면 commentInfo._id  이런식으로

생성한 코멘트의 아이디를 가져와야 합니다 ~ ! 

comment._id 이렇게 하였다면 무조건 커멘트의 아이디가 됩니다 ~ ! 

그리고 혹시  전체 소스를 보고 하고싶다면 
https://github.com/jaewonhimnae/react-youtube-clone

이코드를 보고 하신다면 더 에러가 없을거라 생각이됩니다 ^^  

수고하세요 ~ !