我在sequelize-mysql-express中更新一行的代码如下
// Update an user
userController.update = function(req, res) {
models.user.update(
{
user_name: req.body.name,
user_address: req.body.address,
user_email: req.body.email,
}, {where: {user_id: req.params.id}, returning: true, plain: true})
.then((user)=>{
res.redirect("/users/show/"+user.user_id);
})
.catch((err)=>{
console.log(err);
res.render("../views/users/edit", {user: req.body});
});
};
但是user.user\u id显示了undefined的值如何获取sequelize中更新行的字段和值
2条答案
按热度按时间chhkpiq41#
update
作为第一个参数返回受影响的行数。如果需要受影响的对象,可以使用spread
```// Update an user
userController.update = function(req, res) {
models.user.update(
{
user_name: req.body.name,
user_address: req.body.address,
user_email: req.body.email,
}, {where: {user_id: req.params.id}, returning: true, plain: true})
.spread((affectedCount, affectedRows) => {
// use affectedRows here
})
.catch((err) => {
console.log(err);
res.render("../views/users/edit", {user: req.body});
});
};
gwbalxhn2#
从正式文档开始,这只支持具有
options.returning true
http://docs.sequelizejs.com/class/lib/model.js~model.html#static-方法更新