[英]Express/Sequelize — PUT Route to Update Form results in 500 Server Error
[英]PUT method with express and sequelize
我受困于方法PUT以通过req.params.id更新数据,我试图通过id获取数据,然后在表单表中显示id用户搜索的数据,然后在它们更改了value时它,应该是更新tp数据库
这是我的代码:
router.put('/:id' , (req,res, next)=> {
Student.findById(+req.params.id)
.then(data => {
let arr = data.dataValues;
res.render('edit', {
files : arr
})
})
.catch(err => {
res.status(404).send('something went wrong');
})
const theKey = key => key || undefined
const {first_name, last_name, email } = req.body
let obj = {
first_name : theKey(first_name),
last_name: theKey(last_name),
email: theKey(email),
createdAt: new Date(),
updatedAt: new Date()
}
Student.update(obj,
{ returning: true,
where: {
id : req.params.id
}
})
.then(updated => {
res.send(`updated`)
})
})
在我的app.js中
app.use('/students/edit', editstudent )
在我路由到数据库中的学生列表之后,它不会更新数据,这是我的PUT方法有问题吗?
res.render('edit', {
files : arr
})
.then(updated => {
res.send(`updated`)
})
不更新学生的原因是,在执行更新代码之前,第一个代码块已执行并响应客户端。 我认为您应该在更新学生后呈现页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.