2013-09-25 32 views
7

Tôi đang sử dụng Sequelize làm ORM. Dưới đây là mô hình người dùng của tôi:nhận được lỗi .findOrCreate()

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

Tôi đang tiết kiệm sử dụng:

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 

Nếu email có giá trị (email: "[email protected]"), mọi thứ hoạt động tuyệt vời. Nhưng nếu email không xác thực (như trong ví dụ trên), tôi sẽ gặp lỗi chèn. Làm thế nào để bắt loại lỗi? Phương pháp .error không thể nhận được bất kỳ thông số lỗi nào.

+0

Bạn có thể bật bản ghi SQL cho sequelize. Nó sẽ cung cấp cho bạn truy vấn bạn đang cố gắng chạy gây ra lỗi. cũng @Sriharsha là chính xác mà bạn cần phải specifiy chuỗi args để console.log lỗi –

Trả lời

7

phần tiếp theo sẽ chuyển lỗi làm đại lý cho hàm lỗi.

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

cảm ơn bạn, tôi không thể giải thích lý do tại sao nó đã không làm việc cho tôi trước đây. Ví dụ như lỗi tôi nhận được một chuỗi 'Lỗi: ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Giá trị số nguyên không chính xác: '' cho cột 'coreNumber' ở hàng 1'. Làm thế nào tôi có thể nhận được smth như mã lỗi và id hàng? Tôi cần điều này để xử lý lỗi đầu vào tự động để hiển thị lỗi cho người dùng. Hoặc nó sẽ là tốt hơn để xác nhận đầu vào trước khi chèn vào DB bằng tay? – f1nn

+0

Bạn đang tìm kiếm một regex sẽ kéo ER_TRUNCATED_WRONG_VALUE_FOR_FIELD và hàng 1 ra khỏi chuỗi lỗi đó? – 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: như @Domi chỉ ra, cách tốt hơn là sử dụng 'lây lan' trong thay vì 'rồi'

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

Theo liên kết bạn đã chia sẻ, mã này hoạt động, nhưng đó không phải là cách được khuyến nghị để làm việc với các phương thức trả về nhiều đối số. Bạn có thể sử dụng 'spread (user, created)' thay vì 'then (userAndCreatedInOneArray)' bằng 'findOrCreate', để lưu bạn làm việc với mảng mà bạn gọi' user' (nhưng thực sự không phải là người dùng). – Domi

+2

@Domi điểm tốt, tôi đã sửa đổi câu trả lời – salexch

3

Sequelize 2.0 thay đổi cú pháp và bây giờ sẽ là

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

Trong phần tiếp theo 3.0, bạn nên sử dụng .spread() http://docs.sequelizejs.com/en/latest/api/model/#findorcreateoptions-promiseinstance-created – Kad

Các vấn đề liên quan