작성
·
148
0
Post모델과 Comment모델이 있을 때
db.Post.findAll({
attibutes : ['id', 'description']
})
[
{ id, description },
{ id, description }
....
]
이런 결과값이 나오잖아요
게시물에 포함된 댓글의 수를 구하고 싶습니다.
[
{ id, description, commentCount },
{ id, description, commentCount }
....
]
이런 결과값을 얻고 싶습니다.
현재는
const comments = await post.getComments()
post.commentCount = comments.length
이렇게 구했는데
조인을 통해서 구하는 방법이 있을까요...ㅠㅠ?
답변 1
0
https://stackoverflow.com/questions/37817808/counting-associated-entries-with-sequelize
위 게시글의 답변에서 Sensor를 Comment로 바꾸시면 될 것 같습니다.