首页 > 解决方案 > ValidationError:“列”在空列上不允许为空

问题描述

尝试设置为时passwordResetTokennull我不允许为空错误。如果我手动对数据库运行查询,则没有问题。

更新用户的完整代码如下:

let user = await this.usersRepository.getById(userId);
// ...
const userData = {
    encryptedPassword: await bcrypt.hash(
        newPasswd,
        Number(process.env.BCRYPT_ROUNDS)
    ),
    passwordResetToken: null,
    passwordResetExpires: null
};

user = await this.usersRepository.update(user.id, userData);

我有一个这样定义的模型

const User = sequelize.define(
'user',
{
    forename: DataTypes.STRING,
    surname: DataTypes.STRING,
    email: DataTypes.STRING,
    encryptedPassword: DataTypes.STRING,
    passwordResetToken: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
        validate: {
            notEmpty: false
        }
    },
    passwordResetExpires: {
        type: DataTypes.DATE,
        allowNull: true,
        defaultValue: null,
        validate: {
            isDate: true
        }
    }
},
{
    classMethods: {
        associate() {
            // associations can be defined here
        }
    }
}
);

如您所见allowNull,为真,defaultValue为空并validate: notEmpty设置为假。我错过了什么?

使用续集 v4.42.0

方言“mysql”

标签: mysqlnode.jsexpresssequelize.js

解决方案


这实际上是由我通过 npm 模块结构验证的另一部分引起的。

empty我必须在我的架构中设置为 true,如下所示:

  passwordResetToken: {
    type: String,
    required: false,
    empty: true
  },

推荐阅读