2013-09-25 4 views
7

ORMとしてSequelizeを使用しています。メールが有効な場合get .findOrCreate()エラー

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 

:私は、ユーザーが保存してい

### 
    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 

:ここに私のユーザモデルです(メールは: "[email protected]")、すべてが素晴らしい作品。しかし、電子メールが(上記の例のように)検証に失敗すると、挿入エラーが発生します。エラーの種類を捕捉する方法は? .errorメソッドはエラーパラメータを取得できません。

+0

あなたはsequelizeのためにSQLのログをオンにすることはできますでしょう。実行しようとしているクエリがエラーの原因になります。また@Sriharshaは、あなたが特別なargs文字列をconsole.logエラー –

答えて

7

sequelizeはエラーをパラメーターとしてエラー関数に渡します。

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 
+0

にありがとう、私は以前に私のために働いていなかった理由を説明することはできませんを指定する必要がありますが正しいです。たとえば、エラーとして、私は文字列 'Error:ER_TRUNCATED_WRONG_VALUE_FOR_FIELD:不正な整数値: ''列1の' coreNumber 'を取得します。どのようにエラーコードや行IDのようなsmthを得ることができますか?私は入力エラーを自動的に処理してユーザにエラーを表示する必要があります。または、手動でDBに挿入する前に入力を検証する方が良いでしょうか? – f1nn

+0

ER_TRUNCATED_WRONG_VALUE_FOR_FIELDを引き出し、そのエラー文字列から1行目の正規表現を探していますか? – dankohn

13
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:@Domiが指摘したように、より良い方法は、 'スプレッド' を使用することですに'それから'

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); 
}); 
+1

あなたが共有したリンクによれば、このコードは機能しますが、複数の引数を返すメソッドを扱うのはお勧めできません。 'findOrCreate'で'(userAndCreatedInOneArray) 'の代わりに' spread(user、created) 'を使うことで、' user'と呼ぶ配列での作業を省くことができます。 – Domi

+2

@Domi良い点、私は答えを – salexch

3

Sequelize 2.0変更の構文との代わりは今

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); 
}); 
+2

に変更しました。Sequelize 3.0では、ユーザーに.spread()を推奨します。http://docs.sequelizejs.com/en/latest/api/model/#findorcreateoptions-promiseinstance-created – Kad

関連する問題