【问题标题】:sails.js Logic error in mySQL ORM error catchingsails.js mySQL ORM 错误捕获中的逻辑错误
【发布时间】:2014-06-19 20:59:49
【问题描述】:

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

module.exports = {

    attributes: {

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

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

mySQL ORM 中的逻辑错误。

{ [错误:ER_DUP_ENTRY:键“用户名”的重复条目“用户名”] 代码:'ER _DUP_ENTRY',索引:0 }

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

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

【问题讨论】:

  • done 已被弃用,因为它的用法令人困惑(因为在 promise 库中经常有一个 done)。请改用exec

标签: mysql node.js sails.js waterline


【解决方案1】:

我对 done 回调不是很熟悉,但是你可以尝试使用 promise then handlers

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

【讨论】:

    【解决方案2】:
    User.create({
        username: "username"
    }).exec(function(err, data){
        if(err){
               //the error isn't here
        }
        // data process
    });
    

    【讨论】:

      猜你喜欢
      • 2011-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-18
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多