const student = db.define('student',{ //This is a model
name: {
type: datatype.STRING(40),
allowNull: false
},
age:{
type: datatype.INTEGER(2),
allowNull: false,
defaultValue: -1
}
})
const task = async()=>{
try{
await db.sync()
.then(()=> console.log("Connection Established"))
.catch((err)=> console.log(err))
//Insert a Student
for(let i=0;i<30;i++){
await student.create({
name: (['Tom','Dick','Van Dijk','Muller','Virat','Allison'])[parseInt(Math.random()*10)],
age: 10 * parseInt(Math.random()*10)
})
}
}catch(e){
console.error(e);
}
}
task();
为了执行这个,我收到这样的错误:验证错误项{
消息:'student.name 不能为空',
类型:'notNull Violation',
路径:'名称',
值:空,
起源:'核心',
实例:[学生],
验证器 key :'is_null',
验证器名称:空,
验证器参数:[]
}
student 是一个表,它是一个模型,而 task 是一个向其中插入元素的函数。
最佳答案
parseInt(Math.random()*10)
的输出编号是从 0
到 <=9
。但是 ['Tom','Dick','Van Dijk','Muller','Virat','Allison']
的长度是 6
。因此,如果索引大于 undefined
,则可能存在 5
值。 sequelize 模式中的 name
字段具有 allowNull: false
约束。 undefined
值违反了 NOT NULL
约束。这就是您收到验证错误的原因。
你应该像这样修改它:
parseInt(Math.random() * 6)
它会给你一个从 0
到 <=5
的数字。
关于javascript - 日志中的 ValidationError 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65583675/