ValidationError:“列”不允许放在空的列空
当试图设置passwordResetToken
到null
我得到不允许为空的错误。如果我手动运行对数据库,没有问题的查询。
完整的代码更新用户如下:
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
设置为false。我在想什么?
使用sequelize v4.42.0
方言“MySQL的”
回答如下:这实际上是由NPM模块structure我的验证的另一部分造成的。
我必须设置empty
为true在我的模式是这样的:
passwordResetToken: {
type: String,
required: false,
empty: true
},
ValidationError:“列”不允许放在空的列空
当试图设置passwordResetToken
到null
我得到不允许为空的错误。如果我手动运行对数据库,没有问题的查询。
完整的代码更新用户如下:
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
设置为false。我在想什么?
使用sequelize v4.42.0
方言“MySQL的”
回答如下:这实际上是由NPM模块structure我的验证的另一部分造成的。
我必须设置empty
为true在我的模式是这样的:
passwordResetToken: {
type: String,
required: false,
empty: true
},