codestates / codestates/BookDam
22.01.08 Error Handling
- Dominant language
- JavaScript
- Stars
- 1
- Forks
- 1
- PR merge metrics
- No merged PRs in 30d
Description
### 어떤 에러인가요?
- 유저가 팔로우하는 유저들의 article을 받을 때 내림차순으로 받기 위해서 정렬해야 하는 문제입니다.
### 에러 메시지
- 초기에는 User테이블에서 Article, Follow 테이블이 연관 관계가 되어 있어 User에서 findAll을 했습니다.
- 위의 경우 Article를 최신 순으로 보여주기 위한 정렬이 적용되지 않았습니다.
- Article을 정렬해서 클라이언트로 넘겨주기 위해 Artilce 테이블을 기준으로 findAll을 하고 User테이블을 include하고 User테이블에 Follow테이블을 include하여 해당 문제를 해결할 수 있었습니다.
```js
UserModel.findAll({
attributes: { exclude: ['updatedAt', 'createdAt', 'password'] },
include: [{ model: ArticleModel, attributes: { exclude: ['id', 'updatedAt'] }, order: [['createdAt', 'DESC']] },
{ model: FollowModel, where: { user_Id: id }, attributes: { exclude: ['id', 'createdAt', 'updatedAt'] } }],
raw: true,
// order: [['Articles.createdAt', 'DESC']]
})
```
```js
ArticleModel.findAll({
attributes: { exclude: ['id', 'updatedAt'] },
order: [['createdAt', 'DESC']],
raw: true,
include: [{
model: UserModel,
attributes: { exclude: ['updatedAt', 'createdAt', 'password'] },
required: true,
include: { model: FollowModel, where: { user_Id: id }, attributes: { exclude: ['id', 'createdAt', 'updatedAt'] } }
}]
})
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.