【发布时间】:2019-07-17 21:39:16
【问题描述】:
我卡在了 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 方法有问题吗??
【问题讨论】:
-
尝试记录id
findById(+req.params.id) -
它应该是可编辑的学生表单的形式,当我尝试更改表单输入并提交时,id 不会改变任何我尝试将 update() 移动到 findByID 中的东西,但仍然也行不通
-
检查
req.params.id有值 -
是的,它有值,当我通过 id 查找时,我得到了数据,并且数据在表单上,类似于 pasteboard.co/I2CkCMi.png 表单上的样子
标签: javascript database rest express sequelize.js