gpt4 book ai didi

mysql - sails.js mySQL ORM 错误捕获中的逻辑错误

转载 作者:行者123 更新时间:2023-11-30 00:08:20 25 4
gpt4 key购买 nike

在我的用户模型中,我有:

module.exports = {

attributes: {

username: {
type: 'string',
unique: true,
required: true,
minLength: 5,
maxLength: 15,
alphanumeric: true
}
}
}

现在,当我尝试插入重复的用户名时,我收到以下消息:

Logic error in mySQL ORM.

{ [Error: ER_DUP_ENTRY: Duplicate entry 'username' for key 'username'] code: 'ER _DUP_ENTRY', index: 0 }

现在我的问题是如何捕获此错误,以便向用户显示自定义错误消息?我的创建是这样的:

User.create({
username: "username"
}).done(function(err, data){
if(err){
//the error isn't here
}
});

最佳答案

我对done回调不太熟悉,但你可以尝试使用promise then处理程序

User.create({
username: "username"
})
.then(function(data){
console.log("Success", data);
},function(err){
console.log("My error here", err);
});

关于mysql - sails.js mySQL ORM 错误捕获中的逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24316125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com