• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

Hashtag 가져오기

21.07.30 13:23 작성 조회수 134

0

안녕하세요.

아래는 제가 만든 프로젝트 코드 내용과 강의 내용인데요 (Profile = Post 동일한 모델이라고 생각하시면 됩니다.)

저는 데이터를 가져올 때 해당 해시태그만 가져와지는데 뭐가 문제일까요 .. 

예를들어 노드를 검색하면 노드를 포함한 포스트(프로필) 전체가 가져와지잖아요

#노드#익스프레스 또는 #노드#리액트 이렇게요.

근데 제가 짠 코드에는 그 포스트(프로필)에 #노드만 따라오네요 ㅠㅠ 

const allProfiles = await Profile.findAll({
// where,
// limit : 8,
order: [['createdAt', 'DESC']],
include: [
{
model: Image,
attributes: {
exclude: ['createdAt', 'updatedAt'],
},
},
{
model: Hashtag,
where: { name: decodeURIComponent(req.params.hashtag) },
attributes: {
exclude: ['createdAt', 'updatedAt', 'ProfileTag'],
},
},
],
});

model/hashtag.js

db.Hashtag.belongsToMany(db.Profile, { through: 'ProfileTag' });

model/profile.js

db.Profile.belongsToMany(db.Hashtag, { through: 'ProfileTag' }); 
router.get('/:tag', async (req, res, next) => {
try {
const where = {};
if (parseInt(req.query.lastId, 10)) { // 초기 로딩이 아닐 때
where.id = { [Op.lt]: parseInt(req.query.lastId, 10)}
} // 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
const posts = await Post.findAll({
where,
limit: 10,
include: [
{
model: Hashtag,
where: { name: decodeURIComponent(req.params.tag) },
},
...

답변 1

답변을 작성해보세요.

1

all: true였나, required: true였나 include에 옵션이 있었던 걸로 기억하는데요. 이게 안 된다면

첫 번째 쿼리(위에 보여지는 쿼리)에서는 id만 가져오고, 그 아이디들을 통해서 두 번째 쿼리에서 다시 findAll해서 include해서 다시 만들어내야 합니다.