강의

멘토링

커뮤니티

Inflearn Community Q&A

nan84203857's profile image
nan84203857

asked

[Renewal] Creating NodeBird SNS with React

질문있습니다.

Resolved

Written on

·

128

0

질문1. 회원가입시  백엔드에서는 이 라우터가 실행되잖아요?

router.post('/'isNotLoggedInasync (reqresnext=> { // POST /user/
  try {
      
    const exUser = await User.findOne({
      where: {
        email: req.body.email,
      }
    });
    if (exUser) {
      return res.status(403).send('이미 사용 중인 아이디입니다.');
    }
    const hashedPassword = await bcrypt.hash(req.body.password12);
    await User.create({
      email: req.body.email,
      nickname: req.body.nickname,
      password: hashedPassword,
    });
    res.status(201).send('ok');
  } catch (error) {
    console.error(error);
    next(error); // status 500
  }
});

그후 사가에서 데이터를 받죠

function* signUp(action) {
  try {
    const result = yield call(signUpAPIaction.data);
    // console.log(result);
    yield put({
      type: SIGN_UP_SUCCESS,
    });
  } catch (err) {
    console.error(err);
    yield put({
      type: SIGN_UP_FAILURE,
      error: err.response.data,
    });
  }
}

그런데 이때 SIGH_UP_SUCCESS인지 SIGN_UP_FAILUER인지는 백엔드에서  리턴하는 res.status에서 201인지 403인지를 인식해서  SIGH_UP_SUCCESS,  SIGN_UP_FAILUER 를 정해주는 것인가요?

질문2. 

router.post('/'isNotLoggedInasync (reqresnext=> { // POST /user/
  try {
      
    const exUser = await User.findOne({
      where: {
        email: req.body.email,
      }
    });
    if (exUser) {
      return res.status(403).send('이미 사용 중인 아이디입니다.');
    }
    const hashedPassword = await bcrypt.hash(req.body.password12);
    await User.create({
      email: req.body.email,
      nickname: req.body.nickname,
      password: hashedPassword,
    });
    res.status(201).send('ok');
  } catch (error) {
    console.error(error);
    next(error); // status 500
  }
});

 아이디가 중복될 경우 

 if (exUser) {
      return res.status(403).send('이미 사용 중인 아이디입니다.');
   }

위의 코드가 리턴되어서 403에러를 보내주는것은 알겠는데

catch (error) {
    console.error(error);
    next(error); // status 500
 }

catch (error) 코드는어떤경우일때 코드가 실행되나요?

질문3. 

route 폴더의 user.js인데요 login할때 라우터입니다. 이 코드가 잘 이해되지 않습니다.

const fullUserWithoutPassword = await User.findOne({
        where: { id: user.id },
        attributes: {
          exclude: ['password']
        },
        include: [{
          model: Post,
          attributes: ['id'],
        }, {
          model: User,
          as: 'Followings',
          attributes: ['id'],
        }, {
          model: User,
          as: 'Followers',
          attributes: ['id'],
        }]
     })

코드를 제이슨 형식으로 가져오면

{

  "id": 7,

  "email": "1111@gmail.com",

  "nickname": "빌게이츠",

  "createdAt": "2020-12-24T08:25:12.000Z",

  "updatedAt": "2020-12-25T11:06:45.000Z",

  "Posts": [

    {

      "id": 22

    }

  ],

  "Followings": [

    {

      "id": 8,

      "Follow": {

        "createdAt": "2020-12-25T09:41:18.000Z",

        "updatedAt": "2020-12-25T09:41:18.000Z",

        "FollowingId": 8,

        "FollowerId": 7

      }

    }

  ],

  "Followers": [

    {

      "id": 8,

      "Follow": {

        "createdAt": "2020-12-26T13:31:23.000Z",

        "updatedAt": "2020-12-26T13:31:23.000Z",

        "FollowingId": 7,

        "FollowerId": 8

      }

    },

이런 방식으로 가져오는데 attribute는 id는 모델의id만 가져온다는 거잖아요?

post는 id만 가져왔는데 following과 followers의 모델 user는 attribute id를 썼음에도 id만 가져오지 않고 전체 데이터를 다가져왔는데 제가 뭘 잘못이해했을까요?

nodejsreactexpressreduxNext.js

Answer 1

0

zerocho님의 프로필 이미지
zerocho
Instructor

1. 200번대이면 success가 되고, 나머지는 다 failure가 됩니다.

2. 네트워크나 DB 요청을 하면 항상 예기치 못한 에러가 발생할 수 있습니다. 인터넷 연결이 끊길 수도 있고 서버가 터졌을 수도 있습니다. 그럴 때 catch문에서 걸립니다.

3. 전체 데이터를 다 가져온 게 아닙니다. id와 Follow 데이터 두 개를 가져온 것입니다. id는 attributes에 적어줬으니 가져온 것이고, 다대다 관계에서는 Follow같은 로우 데이터가 추가로 들어 있습니다.

nan84203857's profile image
nan84203857

asked

Ask a question