7

I'm using Sequelize as ORM. Here's my user model:

###
    User model
###
User = exports.User =  globals.sequelize.define "User",
    username: globals.Sequelize.STRING
    email:
        type: globals.Sequelize.STRING
        validate:
            isEmail: true
    hash:     globals.Sequelize.STRING
    salt:     globals.Sequelize.STRING(512)
    fname:    globals.Sequelize.STRING
    lname:    globals.Sequelize.STRING
    country:  globals.Sequelize.STRING

I'm saving user:

globals.models.User.findOrCreate
    username: "johny"
    password: "pass"
    email: "johny93[###]example.com"
.success (user, created)->
    console.log user.values
    res.send 200
.error ->
    console.log err # how to catch this?
    res.send 502

If email is valid (email: "johny93@example.com"), everything works great. But if email fails validation (as in the example above), I get an insertion error. How to catch error type? .error method can't get any error parameters.

Shaharyar
  • 12,254
  • 4
  • 46
  • 66
f1nn
  • 6,989
  • 24
  • 69
  • 92
  • Can you turn on SQL logs for sequelize. It will give you the query you're trying to run that's causing the error. also @Sriharsha is correct that you need to specifiy the args string in order to console.log the error – Zeke Nierenberg Oct 02 '13 at 00:00

3 Answers3

14
 User.findOrCreate({
  where: {
    username: "johny",
    password: "pass",
    email: "johny93[###]example.com"
  },
  defaults: {
    //properties to be created 
  }
}).then(function(user){
  var created = user[1];
  user = user[0];
  console.log(user.values);
}).fail(function(err){
   console.log('Error occured', err);
});

https://github.com/sequelize/sequelize/wiki/Upgrading-to-2.0

EDIT: as @Domi pointed out, better way is to use 'spread' instead of 'then'

User.findOrCreate({
  where: {
    username: "johny",
    password: "pass",
    email: "johny93[###]example.com"
  },
  defaults: {
    //properties to be created 
  }
}).spread(function(user, created){
  console.log(user.values);
}).fail(function(err){
   console.log('Error occured', err);
});
salexch
  • 2,644
  • 1
  • 20
  • 17
  • 1
    According to the link you shared, this code works, but it's not the recommended way to work with methods that return multiple arguments. You can use `spread(user, created)` instead of `then(userAndCreatedInOneArray)` with `findOrCreate`, to save you working with the array you call `user` (but is really not the user). – Domi Jan 13 '15 at 10:42
7

sequelize will pass the error as the paramater to the error function.

JavaScript:

User.findOrCreate({username: "johny",password: "pass",email: "johny93[###]example.com"})
.success(function(user, created){
    console.log(user.values);
    res.send(200);
})
.error(function(err){
   console.log('Error occured' + err);
})

CoffeScript:

globals.models.User.findOrCreate
    username: "johny"
    password: "pass"
    email: "johny93[###]example.com"
.success (user, created)->
    console.log user.values
    res.send 200
.error (error)->
    console.log error # how to catch this?
    res.send 502
Sriharsha
  • 2,373
  • 1
  • 16
  • 20
  • thank you, I can't explain why it had not worked for me before. For example as error I get a string `Error: ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect integer value: '' for column 'coreNumber' at row 1`. How can I get smth like error code and row id? I need this to handle input errors automatically to show error for user. Or will it be better to validate input before insertion to DB manually? – f1nn Sep 28 '13 at 03:27
  • Are you looking for a regex that will pull ER_TRUNCATED_WRONG_VALUE_FOR_FIELD and row 1 out of that error string? – Dan Kohn Oct 01 '13 at 17:36
4

Sequelize 2.0 changes syntax and would now be

User.findOrCreate({
  where: {
    username: 'johny',
    password: 'pass',
    email: 'johny93[###]example.com'
  }
}).then(function (user) {
  res.send(200);
}).catch(function (err) {
  console.log(err);
  res.send(502);
});
skilleo
  • 2,451
  • 1
  • 27
  • 34
  • 2
    In Sequelize 3.0, it is recommended to user .spread() http://docs.sequelizejs.com/en/latest/api/model/#findorcreateoptions-promiseinstance-created – Kad May 01 '16 at 20:22