인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

phenomenon's profile image
phenomenon

asked

NodeJS API Server Built with Test-Driven Development (TDD)

Database and update controller linking

update는 연동이 잘 안되네요 ㅠ

Written on

·

133

1

써주신 로직 그대로 따랐는데(상세한 코드는 좀 다르게 쓰긴 했지만) 작동이 안되어요. user.name === name 이 true가 나오고 있고, unique설정도 모델에서 해줬는데 말입니다. 문제가 뭘까요?ㅠ

const update = (req, res) => {
const id = parseInt(req.params.id, 10)
const name = req.body.name;
if (!name || Number.isNaN(id)) return res.status(400).end();

// models.User.update를 써도 되지만 아래와 같이 findOne과 save()를 쓸 수도 있음
models.User.findOne({ where: { id } })
.then(user => {
if (!user) return res.status(404).end();
user.name = name;
user.save()
.then(() => {
res.status(203).json(user);
})
.catch(err => {
if (err.name = 'SequelizeUniqueConstraintError') {
return res.status(409).end();
}
res.status(500).end();
})
})
};
tddexpressrest-apinodejs

Answer

This question is waiting for answers
Be the first to answer!
phenomenon's profile image
phenomenon

asked

Ask a question