수사 시작.. 다른점을 찾아서...4.26

욱2·2023년 4월 26일
0

NodeJs

목록 보기
7/13

    postCreate = async (userId, _postId) => {
        //
        const t = await sequelize.transaction({
            isolationLevel: Transaction.ISOLATION_LEVELS.READ_COMMITTED, // 트랜잭션 격리 수준을 설정합니다.
        });
        try {
            await Likes.create(
                {
                    UserId: userId,
                    PostId: _postId,
                },
                { transaction: t }
            );

            await Posts.increment(
                { like: +1 },
                { transaction: t, where: { postId: _postId } }
                =====================vs=======================
                { where: { PostId: _postId },
                },
                { transaction: t }
                ===============================================
            );
            console.log("in process");
            await t.commit();
        } catch (transactionError) {
         
            console.error(transactionError);
            await t.rollback();
        }
    };

뭐가 다른걸까요? 삭제 할 때는 왜 두 방식이 가능하고 여기서는 첫번째만 가능할까요?
아직 해결중입니다 1

router.get("/like", authMiddleware, async (req, res) => {
    try {
        const { userId } = res.locals.user;
        const getLiked = await Likes.findAll({
            where: { userId: userId },
            attributes: ["postId"],
        });

        if (getLiked.length < 0)
            throw new Error("404/아직 좋아요를 누른 게시글이 없습니다.");

        const getPosts = await Likes.findAll({
            include: [
                {
                    model: Posts,
                    attributes: [
                        "postId",
                        "userId",
                        "nickname",
                        "title",
                        "createdAt",
                        "updatedAt",
                        "like",
                    ],
                },
            ],
            where: { userId: userId },
            attributes: [],
            order: [[Posts, "like", "desc"]],
        });

        const posts = getPosts.map((post) => ({
            postId: post.postId,
            userId: post.UserId,
            nickname: post.nickname,
            title: post.title,
            createdAt: post.createdAt,
            updatedAt: post.updatedAt,
            likes: post.like,
        }));

        return res.status(200).json({ posts });
    } catch (error) {
        throw new Error(
            error.message || "400/좋아요 게시글 조회에 실패하였습니다."
        );
    }
});


==================================vs==================================

    getLikePost = async (userId) => {
        const getLikePost = await this.likeRepository.getLikePost(userId);

        const posts = getLikePost.map((likePosts) => ({
            postId: likePosts.Post.postId,
            userId: likePosts.Post.UserId,
            nickname: likePosts.Post.nickname,
            title: likePosts.Post.title,
            createdAt: likePosts.Post.createdAt,
            updatedAt: likePosts.Post.updatedAt,
            likes: likePosts.Post.like,
        }));

        return posts;
    };
    
  

왜 첫번째는 Post객체로 안들어가도 되는걸까요?
아직 해결중입니다 2

아 왜 눈물이 나지...재밌는데 왜 ...땀인가..

extra 3

profile
성장하는 날 위한 기록

0개의 댓글