我坚持使用 PUT 方法通过 req.params.id 更新数据,我试图通过 id 获取数据,然后它在表单表中显示 id 用户搜索的数据,然后当他们更改值时它,它应该是更新 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`)
})
学生没有更新的原因是执行了第一个代码块,并在更新代码执行之前响应了客户端。我认为您应该在更新学生后呈现页面。
关于javascript - 使用 express 和 sequelize 的 PUT 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54850029/