且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用节点的 sequelize 更新记录?

更新时间:2023-02-17 20:21:31

我没有用过 Sequelize,但是在阅读它的文档,很明显你正在实例化一个新对象,这就是为什么 Sequelize在数据库中插入一条新记录.

I have not used Sequelize, but after reading its documentation, it's obvious that you are instantiating a new object, that's why Sequelize inserts a new record into the db.

首先您需要搜索该记录,获取它,然后才更改其属性和 更新,例如:

First you need to search for that record, fetch it and only after that change its properties and update it, for example:

Project.find({ where: { title: 'aProject' } })
  .on('success', function (project) {
    // Check if record exists in db
    if (project) {
      project.update({
        title: 'a very different title now'
      })
      .success(function () {})
    }
  })