node.js - 由于变量名与列名不同,在我的创建 Controller 中获取 sequelize 属性 notNull 违规

标签 node.js sequelize.js

我试图使用 bcrypt 将用户添加到我的数据库中,以便我可以拥有他们的密码,但是当我将 req.body 设置为 bcrypt 变量时,我得到了一个 notNull Violation: Users.user_password 不能为空。
这是我用来定义用户表的模型

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Users extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  };
  Users.init({
    user_id:{ 
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
    },
    user_name:{ 
      type: DataTypes.STRING,
      allowNull: false
    },
    user_email:{ 
      type: DataTypes.STRING,
      allowNull: false
    },
    user_password:{ 
      type: DataTypes.STRING,
      allowNull: false
    },
  }, {
    sequelize,
    timestamps: false,
    modelName: 'Users',
    tableName: 'users'
  });
  return Users;
};
这是我用来添加新用户的 Controller 。在底部,当我传入 bcryptPassword 变量而不是在顶部解构的 user_password 时,我得到了 notNull Violation。但是如果我不传入 bcryptPassword 变量,我可以创建一个新用户。有谁知道为什么会这样?有没有办法配置模型以便我可以使用任何变量?任何帮助将不胜感激!
谢谢!
const { Users } = require('../models');
const bcrypt = require('bcrypt');

module.exports = {
    
    createUser: async(req, res) => {
        const { user_name, user_email, user_password } = req.body;

        try {
            
            const salt = await bcrypt.genSalt(10);

            const bcryptPassword = await bcrypt.hash(user_password, salt)
           
            const newUser = await Users.create({ user_name, user_email, bcryptPassword });

            return res.json(newUser);

        } catch (error) {
           
            console.error(error.message);
            return res.status(500).json(error);
        }
    }
}

最佳答案

我在搜索 sequelize 文档时找到了一个可行的答案。
https://sequelize.org/master/manual/getters-setters-virtuals.html
我在我的 user_password 字段中使用了 set() 函数并且有效。每次我创建一个新用户时,密码都会使用 bcrypt 在数据库中散列。
这是修改后的模型代码。变化用星号标记。

'use strict';
*****const bcrypt = require('bcrypt');*****
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Users extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  };
  Users.init({
    user_id:{ 
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
    },
    user_name:{ 
      type: DataTypes.STRING,
      allowNull: false
    },
    user_email:{ 
      type: DataTypes.STRING,
      allowNull: false
    },
    user_password:{ 
      type: DataTypes.STRING,
      allowNull: false,
      *****set(value) {
        const hash = bcrypt.hashSync(value, 10);
        this.setDataValue('user_password', hash);
      }*****
    },
  }, {
    sequelize,
    timestamps: false,
    modelName: 'Users',
    tableName: 'users',
  });
  return Users;
};

关于node.js - 由于变量名与列名不同,在我的创建 Controller 中获取 sequelize 属性 notNull 违规,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67422239/

相关文章:

javascript - 嵌套 forEach 和异步

javascript - Sequelize 如何比较查询中日期的年份

javascript - 优化 Sequelize 中相关表的查询

node.js - 如何获得给定值以在 Hook 中创建 Sequelize 方法

express - 返回一个值的 Handlebars

node.js - Node JS 7.3.0 和 WebStorm 2016.3.2 : IDE always debugs with "--inspect"

javascript - 通过 node.js 观看 FTP 文件夹

authentication - Nodejs,通过https进行身份验证并重定向到http

node.js - 杀死 child_process 的回调

node.js - 使用 ffmpeg,我无法在 node.js spawn 上收到任何错误消息