update와 destroy는 영향을 준 rows의 개수를 반환한다.

하나만 변경or삭제를 한다면 1을 반환해야하고,

update에서 0을 반환한다는 것은 기존에 저장된 값과 새로 요청받은 값이 다르지 않다는 뜻이고,

destroy에서 0을 반환한다는 것은 검색이 되지 않았다는 뜻이다.

기본키로 검색하는 것이 아니라면 여러개 검색될 수 있으니 2이상 값이 반환되는 것도 당연히 그럴 수 있다.

필요하지 않기 때문에 error 응답한다.

 

controllers/post.js

export function updatePost(req, res) {
    const id = req.params.id;

    board.update(req.body, {
        where : {id:id},
    })
    .then((num) => {
        console.log(num);
        if(num == 1) {
            res.status(200).send({
                success: true
            });
        } else if (num == 0) {
            res.status(400).send({
                message: "기존의 값과 같습니다."
            });
        } else {
            res.status(400).send({
                message: "some error occurred"
            });
        }
    });
}

export function deletePost(req, res) {
    const id = req.params.id;

    board.destroy({
        where: {id: id}
    })
    .then((num) => {
        if(num == 1) {
            res.status(200).send({
                success: true
            });
        } else if(num == 0) {
            res.status(400).send({
                success: false,
                message: "post를 찾지 못했습니다."
            })
        } else {
            res.status(400).send({
                success: false,
                message: "some error occurred"
            });
        }
    });  

}

'개발 > Node.js' 카테고리의 다른 글

RESTful API  (0) 2023.11.02
node.js 로그 남기기 - winston  (0) 2023.10.20
데이터베이스 권한 : admin/moderator/user  (0) 2023.09.22
ORM에 대하여  (0) 2023.09.21
node + postgreSQL + Sequelize  (0) 2023.09.20

+ Recent posts