Skip to content

Commit 1bcef98

Browse files
author
guosw
committed
fix: Out of order issue when getting list of comments
1 parent 476b300 commit 1bcef98

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

server/controllers/article.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,17 @@ class ArticleController {
7676
model: CommentModel,
7777
attributes: ['id', 'content', 'createdAt'],
7878
include: [
79-
{ model: UserModel, as: 'user', attributes: ['id', 'username', 'role', 'github'] },
8079
{
8180
model: ReplyModel,
82-
attributes: ['id', 'userId', 'content', 'createdAt'],
83-
include: [{ model: UserModel, as: 'user', attributes: ['id', 'username', 'role', 'github'] }]
84-
}
85-
]
81+
attributes: ['id', 'content', 'createdAt'],
82+
include: [{ model: UserModel, as: 'user', attributes: { exclude: ['updatedAt', 'password'] } }]
83+
},
84+
{ model: UserModel, as: 'user', attributes: { exclude: ['updatedAt', 'password'] } }
85+
],
86+
row: true
8687
}
8788
],
88-
order: [[CommentModel, 'createdAt', 'DESC']], // comment model order
89+
order: [[CommentModel, 'createdAt', 'DESC'], [[CommentModel, ReplyModel, 'createdAt', 'ASC']]], // comment model order
8990
row: true
9091
})
9192

@@ -279,7 +280,7 @@ class ArticleController {
279280
const upStream = fs.createWriteStream(filePath)
280281
reader.pipe(upStream)
281282

282-
reader.on('end', function() {
283+
reader.on('end', function () {
283284
console.log('上传成功')
284285
})
285286
}

server/controllers/discuss.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class DiscussController {
6969
await ReplyModel.create({ userId, articleId, content, commentId })
7070
}
7171
await IpModel.findOrCreate({ where: { ip }, defaults: { userId, ip } })
72-
const list = await DiscussController.fetchCommentList(articleId)
72+
const list = await DiscussController.fetchDiscussList(articleId)
7373

7474
EMAIL_NOTICE.enable && sendingEmail(articleId, list, commentId, userId)
7575

@@ -106,7 +106,7 @@ class DiscussController {
106106
}
107107
}
108108

109-
static async fetchCommentList(articleId) {
109+
static async fetchDiscussList(articleId) {
110110
const data = await CommentModel.findAndCountAll({
111111
where: { articleId },
112112
attributes: ['id', 'content', 'createdAt'],
@@ -119,7 +119,7 @@ class DiscussController {
119119
{ model: UserModel, as: 'user', attributes: { exclude: ['updatedAt', 'password'] } }
120120
],
121121
row: true,
122-
order: [['createdAt', 'DESC']]
122+
order: [['createdAt', 'DESC'], [ReplyModel, 'createdAt', 'ASC']]
123123
})
124124

125125
// 格式化 github

0 commit comments

Comments
 (0)