-
개인과제 : 시퀄라이저 exclude이제 막 슬픔 없이 십오 초 정도가 지났다 2022. 10. 11. 16:24
attribute는 배열로 하나씩 불러낼 수도 있고,
그 안에서 exclude를 통해 일종의 부정신학을 할 수도 있다.
//좋아요 게시글 조회 : 로그인 필요 router.get("/",authMiddleware, async (req, res) => { const {user} = res.locals; console.log(user.userId) // 1 (userId 숫자) try { const likeList = await Likes.findAll({ where: {userId : user.userId}, order: [['likes', 'DESC']], attributes: {exclude: ["createdAt", "updatedAt"]}, //attribute[]로 하나하나 배열 안에 열거해도 된다. attribute 안에서 exclude로 일종의 부정신학을 해도 된다. include: [{ model: Posts, key: 'postId', attributes:['postId', 'title', 'content', 'createdAt', 'updatedAt', 'likes' ] }], }); console.log(likeList); res.status(200).json({ data: likeList }); } catch (error) { const message = `${req.method} ${req.originalUrl} : ${error.message}`; console.log(message); res.status(400).json({ message }); } });
//썬더 클라이언트에 찍힌 응답 { "data": [ { "likeId": 8, "postId": 1, "userId": 1, "nickname": "popopo3", "title": "수정을한다면", "likes": 4, "isLike": true, "Post": { "postId": 1, "title": "수정을한다면", "content": "수정을테스트해봐요", "createdAt": "2022-10-11T02:33:16.000Z", "updatedAt": "2022-10-11T05:49:52.000Z", "likes": 7 } }, { "likeId": 9, "postId": 4, "userId": 1, "nickname": "popopo3", "title": "황제의새마음4", "likes": 3, "isLike": true, "Post": { "postId": 4, "title": "황제의새마음4", "content": "튜링기계에 대해", "createdAt": "2022-10-11T02:33:30.000Z", "updatedAt": "2022-10-11T05:49:31.000Z", "likes": 4 } }, { "likeId": 10, "postId": 2, "userId": 1, "nickname": "popopo3", "title": "황제의새마음1", "likes": 3, "isLike": true, "Post": { "postId": 2, "title": "황제의새마음1", "content": "튜링기계에 대해", "createdAt": "2022-10-11T02:33:21.000Z", "updatedAt": "2022-10-11T05:44:48.000Z", "likes": 4 } }, { "likeId": 11, "postId": 3, "userId": 1, "nickname": "popopo3", "title": "황제의새마음3", "likes": 3, "isLike": true, "Post": { "postId": 3, "title": "황제의새마음3", "content": "튜링기계에 대해", "createdAt": "2022-10-11T02:33:26.000Z", "updatedAt": "2022-10-11T05:44:51.000Z", "likes": 4 } } ] }
그런데 총 likes 숫자가 다르다..... 점검해야할 부분.
https://stackoverflow.com/questions/49095292/exclude-primary-key-attributes-from-a-sequelize-query
Exclude primary key attributes from a sequelize query
I have a sequelize query from multiple tables inner joined together. I need to group them by on a nested include model but the sequelize query throws the primary key every time, even if I mention the
stackoverflow.com
'이제 막 슬픔 없이 십오 초 정도가 지났다' 카테고리의 다른 글
팀과제 : Promise.All (0) 2022.10.29 팀과제 : ORM과 MySQL (0) 2022.10.15 개인과제 : 시퀄라이저 include (0) 2022.10.11 개인과제 : sequelize 세팅 (0) 2022.10.11 개인과제 : ERD 작업 - 좋아요 테이블의 경우 (0) 2022.10.11