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

    • <bdo id='ndcY7'></bdo><ul id='ndcY7'></ul>

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

      <legend id='ndcY7'><style id='ndcY7'><dir id='ndcY7'><q id='ndcY7'></q></dir></style></legend><tfoot id='ndcY7'></tfoot>

      1. Sequelize - 更新记录,并返回结果

        时间:2023-09-03

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

              <tbody id='Vl4u4'></tbody>
            • <legend id='Vl4u4'><style id='Vl4u4'><dir id='Vl4u4'><q id='Vl4u4'></q></dir></style></legend>
              <tfoot id='Vl4u4'></tfoot>
              • <bdo id='Vl4u4'></bdo><ul id='Vl4u4'></ul>

                  <i id='Vl4u4'><tr id='Vl4u4'><dt id='Vl4u4'><q id='Vl4u4'><span id='Vl4u4'><b id='Vl4u4'><form id='Vl4u4'><ins id='Vl4u4'></ins><ul id='Vl4u4'></ul><sub id='Vl4u4'></sub></form><legend id='Vl4u4'></legend><bdo id='Vl4u4'><pre id='Vl4u4'><center id='Vl4u4'></center></pre></bdo></b><th id='Vl4u4'></th></span></q></dt></tr></i><div id='Vl4u4'><tfoot id='Vl4u4'></tfoot><dl id='Vl4u4'><fieldset id='Vl4u4'></fieldset></dl></div>
                  本文介绍了Sequelize - 更新记录,并返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 MySQL 的 sequelize.例如,如果我这样做:

                  I am using sequelize with MySQL. For example if I do:

                  models.People.update({OwnerId: peopleInfo.newuser},
                          {where: {id: peopleInfo.scenario.id}})
                          .then(function (result) {
                              response(result).code(200);
                  
                          }).catch(function (err) {
                          request.server.log(['error'], err.stack);
                         ).code(200);
                      });
                  

                  无论人员模型是否成功更新,我都无法获取信息.变量 result 只是一个只有一个元素的数组,0=1

                  I am not getting information back if the people model was succesfully updated or not. Variable result is just an array with one element, 0=1

                  如何确定记录是否已更新.

                  How can I know for certain that the record was updated or not.

                  推荐答案

                  这就是我认为您正在寻找的.

                  Here's what I think you're looking for.

                  db.connections.update({
                    user: data.username,
                    chatroomID: data.chatroomID
                  }, {
                    where: { socketID: socket.id },
                    returning: true,
                    plain: true
                  })
                  .then(function (result) {
                    console.log(result);   
                    // result = [x] or [x, y]
                    // [x] if you're not using Postgres
                    // [x, y] if you are using Postgres
                  });
                  

                  来自 Sequelize docs:承诺返回一个包含一个或两个元素的数组.第一个元素 x 始终是受影响的行数,而第二个元素 y 是实际受影响的行(仅在带有 options.returning 设置为 true.)

                  From Sequelize docs: The promise returns an array with one or two elements. The first element x is always the number of affected rows, while the second element y is the actual affected rows (only supported in postgres with options.returning set to true.)

                  假设您使用的是 Postgres,您可以使用 result[1].dataValues 访问更新后的对象.

                  Assuming you are using Postgres, you can access the updated object with result[1].dataValues.

                  您必须设置 returning: true 选项来告诉 Sequelize 返回对象.而 plain: true 只是返回对象本身,而不是返回其他可能没用的杂乱元数据.

                  You must set returning: true option to tell Sequelize to return the object. And plain: true is just to return the object itself and not the other messy meta data that might not be useful.

                  这篇关于Sequelize - 更新记录,并返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    <bdo id='mlCvy'></bdo><ul id='mlCvy'></ul>

                    <tfoot id='mlCvy'></tfoot>

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

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