我正在制作一个应用程序,其中包含更新过去的帖子。有一个单独的页面和相应的路线。
app.post("/posts/:id/update", function(req, res){
Post.findAll({
where: {
id: req.params.id
}
}).then(function(foundPost){
var updatePost = foundPost[0]
updatePost.updateAttributes({
description: req.params.description,
author: req.params.author,
image: req.params.image
})
});
res.redirect("/posts")
});
此代码将在数据库中查找该帖子,然后尝试使用从更新页面上的表单收到的数据进行更新。由于某种原因,除了数据库中的“updatedAt”日期之外,该帖子根本没有更新。这是尝试更新帖子后从终端输出的内容:
Executing (default): UPDATE `posts` SET `updatedAt`='2017-07-07 20:50:22' WHERE `id` = 4
它找到了正确的帖子,但没有执行任何操作。
最佳答案
您可以使用模型中可用的更新方法 类似的东西
Posts.update({
image: req.body.image,
title: req.body.title,
// More properties to update
}, {where:{id:req.params.id}})
关于mysql - 使用sequelize和mysql,无法更新内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44979505/