0

In my user model I have:

module.exports = {

    attributes: {

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

Now when I try and insert a duplicate username I get the following message:

Logic error in mySQL ORM.

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

Now my question is how do I catch this error so I can display a custom error message to the user? My create is like so:

User.create({
    username: "username"
}).done(function(err, data){
    if(err){
               //the error isn't here
    }
});
RMK147
  • 191
  • 1
  • 4
  • 15
  • `done` is deprecated, as its usage was confusing (since there's often a `done` in promise libraries). Use `exec` instead. – sgress454 Jun 20 '14 at 16:26

2 Answers2

0

I'm not as familiar with the done callback, but you can try using the promise then handlers

User.create({
    username: "username"
})
.then(function(data){
    console.log("Success", data);
},function(err){
    console.log("My error here", err);
});
Jason Kulatunga
  • 5,814
  • 1
  • 26
  • 50
0
User.create({
    username: "username"
}).exec(function(err, data){
    if(err){
           //the error isn't here
    }
    // data process
});
zieglar
  • 830
  • 8
  • 10