개발일지

개발일지 82일차

index.ys 2023. 6. 25. 14:23

마이페이지 조회 오류

  •  마이페이지를 조회 할때 특정계정이 마이페이지를 조회하지 못하고 무한 로딩이 되는 상황

의심했던 문제 1

토큰의 만료시간 미설정

  • 최초 로그인시 액세스 토큰과 리프레쉬 토큰이 발급 되었을때 만료시간은 제대로 설정되었다
  • 2시간이 지나 액세스 토큰이 만료되었을때 리프레쉬 토큰이 있는지 확인하고 액세스 토큰을 재발급 하는 과정에서 액세스 토큰의 만료시간이 제대로 설정되지 않았을 수도 있다는 가정을 하고 디버깅을 진행했다.

토큰 만료시간 확인

  • 최초 로그인후 액세스 토큰을 복호화 하여 토큰 발행시간과 만료시간을 확인
  • iat: 최초 발생시간으로 14시 11분에 발행되었음
  • exp: 토큰의 만료시간을 설정한것으로 2시간 뒤인 16시 11분으로 정상적으로 설정되어있음
  • 새로운 액세스 토큰이 발급되었을때도 정상적으로 만료시간이 설정됨

의심했던 문제 2

  • sentry에 찍힌 에러로그를 확인했다 마이페이지를 조회했을떄 sentry에 발생한 에러로그는 아래 에러 로그였다
오류!!!: Cannot destructure property 'postId' of 'item.Post' as it is null. undefined TypeError: Cannot destructure property 'postId' of 'item.Post' as it is null.
    at C:\Users\Foryoucom\Desktop\poodaeng-back-end\services\mypage.service.js:49:25
    at Array.map (<anonymous>)
    at myPagesService.getMyBookmark (C:\Users\Foryoucom\Desktop\poodaeng-back-end\services\mypage.service.js:48:34)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async getMyInfo (C:\Users\Foryoucom\Desktop\poodaeng-back-end\controllers\mypage.controller.js:10:35) {
  failedApi: '내가 북마크한 게시글 조회'
}

에러가 발생한 코드

  • 내가 북마크한 게시글을 모두 조회할때 map메서드를 실행할 postId가 없을때 에러가 발생하는 상황이었다
getMyBookmark = async (userId) => {
        try {
            const getMyBookmark = await this.mypageRepository.getMyBookmark(userId)
            if (!userId) {
                throw new Error("401/마이페이지 권한이 없습니다.")
            }
            if (!getMyBookmark) {
                throw new Error("401/데이터가 존재하지 않습니다.")
            }

            return getMyBookmark.map((item) => {
                const { postId, title, content, lostPhotoUrl, createdAt, updatedAt } = item.Post;
                return {
                    postId,
                    title,
                    content,
                    lostPhotoUrl,
                    createdAt,
                    updatedAt
                };
            });

        } catch (error) {
            error.failedApi = "내가 북마크한 게시글 조회";
            throw error;
        }
    };

수정한 코드

  • 내가 북마크한 게시글이 없을떄 (getMyBookmark.length === 0 일떄)  조건문으로 데이터가 없다는 메세지를 클라이언트에 전달해주는 처리를 하여 에러를 수정함
 getMyBookmark = async (userId) => {
        try {
            const getMyBookmark = await this.mypageRepository.getMyBookmark(userId)
            if (!userId) {
                throw new Error("401/마이페이지 권한이 없습니다.")
            }
            if (getMyBookmark.length === 0) {
                return { message: "데이터 없쩌" }
            }

            const result = await Promise.all(
                getMyBookmark.map(({ bookmarkId, Post: { dataValues: { postId, title, content, lostPhotoUrl, createdAt, updatedAt } } }) => ({
                    bookmarkId,
                    postId,
                    title,
                    content,
                    lostPhotoUrl,
                    createdAt,
                    updatedAt,
                }))
            );
            return result
        } catch (error) {
            error.failedApi = "내가 북마크한 게시글 조회";
            throw error;
        }
    };