我使用 sequelize.js nodejs 作为后端。当存在验证错误/唯一错误/空错误时
这是 Sequelize 验证错误。基本上我想得到错误作为 Node js 错误。它显示错误中的所有内容。我只想显示路径和消息。
当前错误显示
{
"name": "SequelizeValidationError",
"message": "Validation error: Validation notEmpty failed",
"errors": [
{
"message": "Validation notEmpty failed",
"type": "Validation error",
"path": "name",
"value": {},
"__raw": {}
}
]
}
我要这个
{
"name":"The field cannot be empty",
"other_field":"custom error message"
}
最佳答案
您可以添加自定义错误消息以在 Sequelize 中进行验证。请引用this
class User extends Model {}
User.init({
name: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notNull: {
msg: 'Please enter your name'
}
}
}
}, { sequelize });
关于node.js - 自定义验证错误空错误唯一错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60771183/