try ...catch문 `e.response.data` 사용하기

마데슾 : My Dev Space·2020년 7월 20일
0
router.post('/', async (req, res, next) => {
  // POST /api/user 회원가입
  try {
    const exUser = await db.User.findOne({
      where: {
        email: req.body.email,
      },
    });

    const affliationId = await db.Affliations.findOne({
      where: {
        name: req.body.affliations,
      },
    });

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

회원가입시 입력한 emailUser 테이블에 존재한다면 res.status(403).send('이미 사용중인 아이디입니다.') 이 코드를 반환한다.

// sagas
try {
    yield call(signUpAPI, action.data);
    yield put({
      type: SIGN_UP_SUCCESS,
    });
  } catch (e) {
    // loginAPI 실패
    console.log('e ? ', e);
    console.log('e ? ', e.response.data);
    yield put({
      type: SIGN_UP_FAILURE,
      error: e.response.data,
    });
  }

[ console.log ] e / e.response.data ▼

[ console.log ] e.response ▼

res.status(403).send('이미 사용중인 아이디입니다.'); 를 반환하게 되면 catche.response.data에 담기게된다.

// reducer
case SIGN_UP_FAILURE: {
  draft.isSigningUp = false;
  draft.isSignedUp = false;
  console.log('action ? ', action);
  draft.signUpErrorReason = action.error;
  break;
}

action.error에 담긴 문구를 받아 이메일이 중복되면 alert에 넣어 띄워주면 된다!

profile
👩🏻‍💻 🚀

0개의 댓글