• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    해결됨

models 최신 문법으로 변경 후

20.10.04 16:58 작성 조회수 139

0

index 페이지에 /Posts 요청에서 

sequelizeeagerloadingerror Image is not associated to Comment

에러가 나고 로그인도 안되요 models 폴더 말고 다른 파일도 수정해야하는 곳이 있나요?

답변 2

·

답변을 작성해보세요.

0

tkdals9048님의 프로필

tkdals9048

질문자

2020.10.07

이런.. import 를 잘못해오고 있었어요 답변 감사합니다!

0

Comment랑 Image랑 혹시 연결하셨나요? 두 테이블은 서로 관계가 없습니다. /posts(게시글 불러오는 코드)에서 include 부분도 잘 살펴보세요.


router.get('/', async (req, res, next) => { // GET /posts
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,
order: [
['createdAt', 'DESC'],
[Comment, 'createdAt', 'DESC'],
],
include: [{
model: User,
attributes: ['id', 'nickname'],
}, {
model: Image,
}, {
model: Comment,
include: [{
model: User,
attributes: ['id', 'nickname'],
}],
}, {
model: User, // 좋아요 누른 사람
as: 'Likers',
attributes: ['id'],
}, {
model: Post,
as: 'Retweet',
include: [{
model: User,
attributes: ['id', 'nickname'],
}, {
model: Image,
}]
}],
});
res.status(200).json(posts);
} catch (error) {
console.error(error);
next(error);
}
});