강의

멘토링

로드맵

Inflearn Community Q&A

cmxntkxkd2672's profile image
cmxntkxkd2672

asked

[Renewal] Creating NodeBird SNS with React

Like this post

include질문이요

Written on

·

168

0

    const post = await Post.create({
      content: req.body.content,
      UserId: req.user.id, // deserializeUser 실행되면서 req.user에 값을 넣어준다.
  });   
const fullPost = await Post.findOne({
      where: {
        id: post.id,
      },
      include: [
        {
          model: Image,
        },
        {
          model: Comment,
          include: [
            {
              model: User, // 댓글 작성자
              attributes: ["id", "nickname"],
            },
          ],
        },
        {
          model: User, // 게시글 작성자
          attributes: ["id", "nickname"],
      },
 
include한 모델들이 어떻게 post에 관한 정보들을
가져 올 수 있는지 궁금합니다.
예를들어 제 생각에는 post에 관한 모델 User를
가지고 올려면 User에
User.findOne({where:{PostId: post.id}})처럼
post에 관한 정보가 있어야 된다고 생각하는데 그런 속성
없이 가져 올 수 있는게 이해가 안갑니다.
Next.jsreactreduxexpressnodejs

Answer 2

0

dsfsdf님의 프로필 이미지
dsfsdf
Questioner

    const fullPost = await Post.findOne({
      where: {
        id: post.id,
    },
include: [
     {
        model: User, // 게시글 작성자
        attributes: ["id", "nickname"],
      },
    ],
include에서 model User에는 수많은 유저들이 있을텐데
그중에서 어떻게 post.id에 관한 게시글을 작성한 User를
찾을 수 있는 것인지에 대한 의문입니다.
Post.associate랑 연관 되어 있는 것 같은데 잘 모르겠습니다.
zerocho님의 프로필 이미지
zerocho
Instructor

그걸 시퀄라이즈가 알아서 해주는 겁니다. post에는 userId가 적혀있고, 시퀄라이즈에 post-user이 belongsTo관계로 엮어있으니까요. JOIN문을 알아서 만들어줍니다.

0

zerocho님의 프로필 이미지
zerocho
Instructor

const fullPost = await Post.findOne({
      where: {
        id: post.id,
    },
 
질문이 잘 이해가 안 되는 것이, 여기에 저희가 post.id를 넣어주었는데요?
cmxntkxkd2672's profile image
cmxntkxkd2672

asked

Ask a question