<tfoot id='ql0tp'></tfoot>

    1. <i id='ql0tp'><tr id='ql0tp'><dt id='ql0tp'><q id='ql0tp'><span id='ql0tp'><b id='ql0tp'><form id='ql0tp'><ins id='ql0tp'></ins><ul id='ql0tp'></ul><sub id='ql0tp'></sub></form><legend id='ql0tp'></legend><bdo id='ql0tp'><pre id='ql0tp'><center id='ql0tp'></center></pre></bdo></b><th id='ql0tp'></th></span></q></dt></tr></i><div id='ql0tp'><tfoot id='ql0tp'></tfoot><dl id='ql0tp'><fieldset id='ql0tp'></fieldset></dl></div>
        <bdo id='ql0tp'></bdo><ul id='ql0tp'></ul>

    2. <small id='ql0tp'></small><noframes id='ql0tp'>

      1. <legend id='ql0tp'><style id='ql0tp'><dir id='ql0tp'><q id='ql0tp'></q></dir></style></legend>

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

        How to update a record using sequelize for node?(如何使用节点的 sequelize 更新记录?)
          <i id='ztcVn'><tr id='ztcVn'><dt id='ztcVn'><q id='ztcVn'><span id='ztcVn'><b id='ztcVn'><form id='ztcVn'><ins id='ztcVn'></ins><ul id='ztcVn'></ul><sub id='ztcVn'></sub></form><legend id='ztcVn'></legend><bdo id='ztcVn'><pre id='ztcVn'><center id='ztcVn'></center></pre></bdo></b><th id='ztcVn'></th></span></q></dt></tr></i><div id='ztcVn'><tfoot id='ztcVn'></tfoot><dl id='ztcVn'><fieldset id='ztcVn'></fieldset></dl></div>
          • <tfoot id='ztcVn'></tfoot>
            <legend id='ztcVn'><style id='ztcVn'><dir id='ztcVn'><q id='ztcVn'></q></dir></style></legend>

                <bdo id='ztcVn'></bdo><ul id='ztcVn'></ul>
                  <tbody id='ztcVn'></tbody>

                <small id='ztcVn'></small><noframes id='ztcVn'>

                1. 本文介绍了如何使用节点的 sequelize 更新记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 NodeJS、express、express-resource 和 Sequelize 创建一个 RESTful API,用于管理存储在 MySQL 数据库中的数据集.

                  I'm creating a RESTful API with NodeJS, express, express-resource, and Sequelize that is used to manage datasets stored in a MySQL database.

                  我正在尝试弄清楚如何使用 Sequelize 正确更新记录.

                  I'm trying to figure out how to properly update a record using Sequelize.

                  我创建了一个模型:

                  module.exports = function (sequelize, DataTypes) {
                    return sequelize.define('Locale', {
                      id: {
                        type: DataTypes.INTEGER,
                        autoIncrement: true,
                        primaryKey: true
                      },
                      locale: {
                        type: DataTypes.STRING,
                        allowNull: false,
                        unique: true,
                        validate: {
                          len: 2
                        }
                      },
                      visible: {
                        type: DataTypes.BOOLEAN,
                        defaultValue: 1
                      }
                    })
                  }
                  

                  然后,在我的资源控制器中,我定义了一个更新操作.

                  Then, in my resource controller, I define an update action.

                  在这里,我希望能够更新 id 与 req.params 变量匹配的记录.

                  In here I want to be able to update the record where the id matches a req.params variable.

                  首先我建立一个模型,然后我使用 updateAttributes 方法来更新记录.

                  First I build a model and then I use the updateAttributes method to update the record.

                  const Sequelize = require('sequelize')
                  const { dbconfig } = require('../config.js')
                  
                  // Initialize database connection
                  const sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password)
                  
                  // Locale model
                  const Locales = sequelize.import(__dirname + './models/Locale')
                  
                  // Create schema if necessary
                  Locales.sync()
                  
                  
                  /**
                   * PUT /locale/:id
                   */
                  
                  exports.update = function (req, res) {
                    if (req.body.name) {
                      const loc = Locales.build()
                  
                      loc.updateAttributes({
                        locale: req.body.name
                      })
                        .on('success', id => {
                          res.json({
                            success: true
                          }, 200)
                        })
                        .on('failure', error => {
                          throw new Error(error)
                        })
                    }
                    else
                      throw new Error('Data not provided')
                  }
                  

                  现在,这实际上并没有像我期望的那样产生更新查询.

                  Now, this does not actually produce an update query as I would expect.

                  而是执行插入查询:

                  INSERT INTO `Locales`(`id`, `locale`, `createdAt`, `updatedAt`, `visible`)
                  VALUES ('1', 'us', '2011-11-16 05:26:09', '2011-11-16 05:26:15', 1)
                  

                  所以我的问题是:使用 Sequelize ORM 更新记录的正确方法是什么?

                  So my question is: What is the proper way to update a record using Sequelize ORM?

                  推荐答案

                  我没有用过 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 () {})
                      }
                    })
                  

                  这篇关于如何使用节点的 sequelize 更新记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to provide a mysql database connection in single file in nodejs(如何在 nodejs 中的单个文件中提供 mysql 数据库连接)
                  How can I use a single mssql connection pool across several routes in an Express 4 web application?(如何在 Express 4 Web 应用程序中跨多个路由使用单个 mssql 连接池?)
                  Looping Over Result Sets in MySQL(在 MySQL 中循环结果集)
                  How to use sqlite3 module with electron?(如何在电子中使用 sqlite3 模块?)
                  SQL Server Express vs MS Access(SQL Server Express 与 MS Access)
                  Writing an SQL query to SELECT item from the following table(编写 SQL 查询以从下表中选择项目)
                    <tbody id='gK8pT'></tbody>

                  <tfoot id='gK8pT'></tfoot>

                      • <bdo id='gK8pT'></bdo><ul id='gK8pT'></ul>
                          <legend id='gK8pT'><style id='gK8pT'><dir id='gK8pT'><q id='gK8pT'></q></dir></style></legend>

                        1. <small id='gK8pT'></small><noframes id='gK8pT'>

                          • <i id='gK8pT'><tr id='gK8pT'><dt id='gK8pT'><q id='gK8pT'><span id='gK8pT'><b id='gK8pT'><form id='gK8pT'><ins id='gK8pT'></ins><ul id='gK8pT'></ul><sub id='gK8pT'></sub></form><legend id='gK8pT'></legend><bdo id='gK8pT'><pre id='gK8pT'><center id='gK8pT'></center></pre></bdo></b><th id='gK8pT'></th></span></q></dt></tr></i><div id='gK8pT'><tfoot id='gK8pT'></tfoot><dl id='gK8pT'><fieldset id='gK8pT'></fieldset></dl></div>