【发布时间】:2020-04-06 14:37:13
【问题描述】:
当我进行以下查询以获取带有选项的投票并且每个选项都有投票计数时。但它会让人费解。
router.get("/", (req, res) => {
Poll.findAll({
where: { subjectId: req.query.subjectId },
include: {
model: PollOption,
attributes: {
include: [[Sequelize.fn("COUNT", "pollVote.id"), "voteCount"]]
},
required: false,
include: {
model: PollVote,
// attributes: [],
required: false
}
},
group:['pollOptions.id']
}).then(poll => {
console.log(poll);
res.send({ poll });
});
});
我得到这个作为回应
{
"poll": [
//Array of the poll
{
"id": 3,
"module_id": "mbbs",
"question": "Test Question Poll",
"pollOptions": [
{
"id": 1,
"pollId": 3,
"option": "Option 1",
"voteCount": 1,
"pollVotes": [
{
"id": 6,
"pollOptionId": 1,
"pollId": 3,
"userId": 3
}
]
},
{
"id": 2,
"pollId": 3,
"userId": 120,
"option": "Option 2",
"voteCount": 2,
//Vote must be 1 but return 2
"pollVotes": [
{
"id": 2,
"pollOptionId": 2,
"pollId": 3,
"userId": 1
}
]
}
]
},
{
"id": 4,
"module_id": "mbbs",
"question": "Test Question Poll 4",
"subjectId": 1,
"pollOptions": [
{
"id": 3,
"pollId": 4,
"option": "Option 1sd",
"voteCount": 1,
"pollVotes": []
}
]
}
]
}
在哪里 voteCount 是错误的。 我怎样才能使它正确。
Sql查询如下
选择poll.id, poll.module_id, poll.question, poll.userId, userId, poll.poll.poll.@9876543333 @, poll.expireDate, pollOptions.id AS pollOptions.id, pollOptions.pollId AS pollOptions.pollId, pollOptions.@987654344.@AS @987654345., @987654344. option AS pollOptions.option, COUNT('pollVote.id') AS pollOptions.voteCount, pollOptions->pollVotes.id AS pollOptions.pollVotes.id, pollOptions->pollVotes.pollOptionId AS pollOptions.pollVotes.pollOptionId, @987654。 987654357@aspollOptions.pollVotes.pollId,pollOptions->pollVotes。userIdaspollOptions.pollVotes.userIdpollfrompollas@987654363 987654369@左OUTER JOINpollVoteaspollOptions->pollVotesonpollOptions。id=pollOptions->pollVotes=pollOptions->pollVotes。
【问题讨论】:
-
可以分享一下 Sequelize 生成的查询吗?
-
更新帖子
标签: node.js reactjs sequelize.js