강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của ssp57460900
ssp57460900

câu hỏi đã được viết

Tạo NodeBird SNS bằng React

6-17. Theo dõi, hủy theo dõi

동작 오류 질문입니다.

Đã giải quyết

Viết

·

566

0

팔로우 언팔로우 기능이 안되서 확인해보니

네트워크 탭에 SequelizeUniqueConstraintError: Validation error

    at Query.formatError (C:\Users\Desktop\react-nodebird\back\node_modules\sequelize\lib\dialects\mysql\query.js:223:16)
   at Query.handler [as onResult] (C:\Users\Desktop\react-nodebird\back\node_modules\sequelize\lib\dialects\mysql\query.js:51:23)
   at Query.execute (C:\Users\Desktop\react-nodebird\back\node_modules\mysql2\lib\commands\command.js:30:14)
   at Connection.handlePacket (C:\Users\Desktop\react-nodebird\back\node_modules\mysql2\lib\connection.js:417:32)
   at PacketParser.Connection.packetParser.p [as onPacket] (C:\Users\Desktop\react-nodebird\back\node_modules\mysql2\lib\connection.js:75:12)
   at PacketParser.executeStart (C:\Users\Desktop\react-nodebird\back\node_modules\mysql2\lib\packet_parser.js:75:16)
   at Socket.Connection.stream.on.data (C:\Users\Desktop\react-nodebird\back\node_modules\mysql2\lib\connection.js:82:25)
   at Socket.emit (events.js:198:13)
   at addChunk (_stream_readable.js:288:12)
   at readableAddChunk (_stream_readable.js:269:11)
   at Socket.Readable.push (_stream_readable.js:224:10)
   at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)

위에 내용과 같이 출력됩니다. 리덕스에서 팔로우 클릭시 FOLLOW_USER_FAULURE라고 출력됩니다.
javascriptreact

Câu trả lời 7

0

zerocho님의 프로필 이미지
zerocho
Người chia sẻ kiến thức

axios.get과 axios.delete는 데이터가 없습니다(요청의 body가 없습니다) 그래서 두 번째 인수는 데이터가 아니라 옵션 객체입니다. post와 put patch는 두 번째 인수가 데이터고 세 번째 인수가 옵션입니다.

0

s s님의 프로필 이미지
s s
Người đặt câu hỏi

오류의 원인을 찾았습니다 

//언팔로우
function unfollowAPI(userId) {
  // 서버에 요청을 보내는 부분
  return axios.delete(`/user/${userId}/follow`, {}, {
    withCredentials: true,
  });
}
//팔로우
function followAPI(userId) {
  // 서버에 요청을 보내는 부분
  return axios.post(`/user/${userId}/follow`, {}, {
    withCredentials: true,
  });
}

이 부분에서 unfollowAPI의 axios.delete 부분의 {} 이부분이 문제가 있었네요 데이터 보내는게 없어도 빈 배열로 데이터

보내야 되는줄 알았는데 unfollow쪽에 저렇게 보내면 에러가 출력되더라고요 이유가 뭔가요??

0

zerocho님의 프로필 이미지
zerocho
Người chia sẻ kiến thức

지금 어떤 코드에서 오타를 내셨는데(또는 제 강좌 코드랑 다르게 하셨는데), 이 부분만 보고서는 찾기가 너무나 어렵습니다. ㅠㅠ

0

s s님의 프로필 이미지
s s
Người đặt câu hỏi

이부분 말씀하시는건가요? 해줬습니다.

//언팔로우
function unfollowAPI(userId) {
  // 서버에 요청을 보내는 부분
  return axios.delete(`/user/${userId}/follow`, {}, {
    withCredentials: true,
  });
}
//팔로우
function followAPI(userId) {
  // 서버에 요청을 보내는 부분
  return axios.post(`/user/${userId}/follow`, {}, {
    withCredentials: true,
  });
}

0

zerocho님의 프로필 이미지
zerocho
Người chia sẻ kiến thức

axios에서 withCredentials: true 해주셨나요?

0

s s님의 프로필 이미지
s s
Người đặt câu hỏi

로그인이 되어있는 상태인데 로그인이 필요합니다라고 출력되네요.

reducers/user.js 의 소스입니다.

case FOLLOW_USER_REQUEST: {
        return {
          ...state,
        };
      }
      case FOLLOW_USER_SUCCESS: {
          return {
            ...state,
            me: {
              ...state.me,
              Followings: [{ id: action.data }, ...state.me.Followings],
            },
          };
      }
      case FOLLOW_USER_FAILURE: {
        return {
          ...state,
        };
      }
      case UNFOLLOW_USER_REQUEST: {
        return {
          ...state,
        };
      }
      case UNFOLLOW_USER_SUCCESS: {
        return {
          ...state,
          me: {
            ...state.me,
            // Followings: [...state.me.Followings].filter(v => v.id !== action.data),
            Followings: state.me.Followings.filter(v => v.id !== action.data),
          },
        };
      }
      case UNFOLLOW_USER_FAILURE: {
        return {
          ...state,
        };
     }

sagas/user.js의 소스입니다.

//=============================================
//팔로우
function followAPI(userId) {
  // 서버에 요청을 보내는 부분
  return axios.post(`/user/${userId}/follow`, {}, {
    withCredentials: true,
  });
}

function* follow(action) {
  try {
    // yield call(loadUserAPI);
    const result = yield call(followAPIaction.data);
    yield put({ // put은 dispatch 동일
      type: FOLLOW_USER_SUCCESS,
      data: result.data,
    });
  } catch (e) { // loginAPI 실패
    console.error(e);
    yield put({
      type: FOLLOW_USER_FAILURE,
      error: e,
    });
  }
}

function* watchFollow() {
  yield takeEvery(FOLLOW_USER_REQUESTfollow);
}

//=============================================
//언팔로우
function unfollowAPI(userId) {
  // 서버에 요청을 보내는 부분
  return axios.delete(`/user/${userId}/follow`, {}, {
    withCredentials: true,
  });
}

function* unfollow(action) {
  try {
    // yield call(loadUserAPI);
    const result = yield call(unfollowAPIaction.data);
    yield put({ // put은 dispatch 동일
      type: UNFOLLOW_USER_SUCCESS,
      data: result.data,
    });
  } catch (e) { // loginAPI 실패
    console.error(e);
    yield put({
      type: UNFOLLOW_USER_FAILURE,
      error: e,
    });
  }
}

function* watchUnfollow() {
  yield takeEvery(UNFOLLOW_USER_REQUESTunfollow);
}

back/routes/user.js의 소스입니다.

router.post('/:id/follow'isLoggedInasync (reqresnext=> {
    try {
      const me = await db.User.findOne({
        where: { id: req.user.id },
      });
      await me.addFollowing(req.params.id);
      res.send(req.params.id);
    } catch (e) {
      console.error(e);
      next(e);
    }
  });
  
router.delete('/:id/follow'isLoggedInasync (reqresnext=> {
    try {
      const me = await db.User.findOne({
        where: { id: req.user.id },
      });
      await me.removeFollowing(req.params.id);
      res.send(req.params.id);
    } catch (e) {
      console.error(e);
      next(e);
    }
 });

0

zerocho님의 프로필 이미지
zerocho
Người chia sẻ kiến thức

에러가 짤렸습니다. 서버 로그에서 확인해주세요. 서버쪽 에러는 서버에서 확인하셔야 합니다.

Hình ảnh hồ sơ của ssp57460900
ssp57460900

câu hỏi đã được viết

Đặt câu hỏi