mysql - 使用sequelize和mysql,无法更新内容

标签 mysql node.js express sequelize.js

我正在制作一个应用程序,其中包含更新过去的帖子。有一个单独的页面和相应的路线。

  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/

相关文章:

PHPStorm 表单处理 : PHP variable not echoing, 也不发布到 sql

php - 最佳实践 : Upload a picture for non existing Task

javascript - 如何对表示整数的字符串数组求和

javascript - 类型错误 : Class constructor DatabaseError cannot be invoked without 'new'

node.js - Azure 上的 Express App 无法加载静态文件,状态 500

mysql - 在 JavaFX 应用程序中打开和关闭连接

php - 根据sql查询结果创建多维数组

node.js - Mongoose 错误 : Can't use $or with String

node.js - 如何在不读取标准输入的情况下使用 `npm login` 设置 npm 凭据?

node.js - 如何在 sequelize 中添加复合外键?