尽管操作成功,但更新操作将表字段设置为空字符串

问题描述 投票:0回答:1

此功能根据http请求中收到的信息更新管理表。注意记录的信息。

var updateAdminById = function(new_admin_data, callback) {
  logger.info("new_admin_data: " + JSON.stringify(new_admin_data));
  logger.info("new_admin_data.id: " + new_admin_data.id);
  /**new_admin_data: {"email":"[email protected]","mot_de_passe":"PasswordJdida","role":"operateur_sav","id":"71","nom":"Hamadi Jdid"} */

  models.admin
    .update(new_admin_data, {
      where: { id: new_admin_data.id },
      returning: true // To return the affected row
    })
    .then(function(result) {
      logger.info("Updated admin is: " + new_admin_data.id);
      console.log(result[1][0].dataValues);
      // Up until here everything is fine!
      /**
       * 2020-02-14T15:58:21+0100 <info> AdminServices.js:196  Updated admin is: 71
        {
          id: 71,
          nom: 'Hamadi Jdid',
          email: '[email protected]',
          mot_de_passe: 'PasswordJdida',
          last_login: null,
          role: 'operateur_sav',
          createdAt: 2020-02-14T14:51:40.571Z,
          updatedAt: 2020-02-14T14:58:21.090Z
        }
       */
      // But, when I check in the database, I find that the password field is empty
      // Something is setting the password to empty String for some reason
      callback({
        update: true,
        admin: result[1][0] //Contains updated admin info
      });
    })
    .catch(function(error) {
      logger.error("Admin failed to update ", error);
      callback({
        update: false,
        error
      });
    });
};  

这是一个表项,我将使用POSTMAN进行修改:

{
    "id": 72,
    "nom": "testUpdateButton",
    "email": "[email protected]",
    "mot_de_passe": "951357159357",
    "role": "operateur_sav",
    "last_login": null,
    "createdAt": "2020-02-14T15:11:21.860Z",
    "updatedAt": "2020-02-14T15:11:21.860Z"
}  

enter image description here请注意,该信息已在后端成功接收:

new_admin_data: {"id":"71","nom":"newName","email":"[email protected]","mot_de_passe":"newName","role":"operateur_sav"}

在此处登录:

  logger.info("new_admin_data: " + JSON.stringify(new_admin_data));
  logger.info("new_admin_data.id: " + new_admin_data.id);

还请注意,该操作已成功完成。这是更新后的记录响应:

2020-02-14T16:12:34+0100 <info> AdminServices.js:196  Updated admin is: 71
{
  id: 71,
  nom: 'newName',
  email: '[email protected]',
  mot_de_passe: 'newName',
  last_login: null,
  role: 'operateur_sav',
  createdAt: 2020-02-14T14:51:40.571Z,
  updatedAt: 2020-02-14T15:12:34.889Z
}  

我正在记录的内容在这里更新了表项之后,会有响应:

  models.admin
    .update(new_admin_data, {
      where: { id: new_admin_data.id },
      returning: true // To return the affected row
    })
    .then(function(result) {
      logger.info("Updated admin is: " + new_admin_data.id);
      console.log(result[1][0].dataValues);

关于这一点的奇怪之处在于,它成功地更新了除设置为空字符串mot_de_passe字段以外的所有字段!从数据库检索该表条目后,查看结果:

 {
        "id": 71,
        "nom": "newName",
        "email": "[email protected]",
        "mot_de_passe": "",
        "role": "operateur_sav",
        "last_login": null,
        "createdAt": "2020-02-14T14:51:40.571Z",
        "updatedAt": "2020-02-14T15:12:34.889Z"
    }

我已经检查了更新操作的所有流程,似乎一切正常。因此,我不知道为什么特别将字段mot_de_passe设置为空字符串尽管更新后的序列化响应告诉我所有字段更新都已成功完成这一事实。我有learned,Sequelize可以将NULL值设置为空字符串。但是,由于可以肯定的是,由于进行了日志记录,我在数据库请求中发送的mot_de_passe字段不是NULL,因此我认为这不是解释!

node.js postgresql express sequelize.js
1个回答
0
投票

默认情况下,Sequelize更新所有字段,因此将选项中的字段传递给更新功能。

参考:https://sequelize.org/master/class/lib/model.js~Model.html#static-method-update

© www.soinside.com 2019 - 2024. All rights reserved.