질문1. 회원가입시 백엔드에서는 이 라우터가 실행되잖아요?
router.post('/', isNotLoggedIn, async (req, res, next) => { // 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.password, 12);
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(signUpAPI, action.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('/', isNotLoggedIn, async (req, res, next) => { // 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.password, 12);
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만 가져오지 않고 전체 데이터를 다가져왔는데 제가 뭘 잘못이해했을까요?