2015-04-16 16 views
5

trong khi quy tắc xác thực email không thành công trên mô-đun của sails.js, máy chủ bị lỗi. Đây là đoạn mô-đun của tôi: Địa chỉ email // của người dùng"email" quy tắc xác thực lỗi máy chủ sails - Mongo với Sails.js

email: { 
    type: 'string', 
    email: true, 
    required: true, 
    unique: true 
}, 

Và các lỗi như sau:

err: Lỗi (E_VALIDATION) :: 1 thuộc tính là không hợp lệ tại WLValidationError. WLError (C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ waterline \ lib \ waterline \ error \ WLError.js: 26: 15) tại WLValidationError mới (C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ waterline \ lib \ waterline \ error \ WLValidationError.js: 20: 28) tại C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ waterline \ lib \ waterline \ query \ validate.js: 45: 43 at allValidationsChecked (C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ waterline \ lib \ waterline \ core \ validations.js: 203: 5) khi hoàn tất (C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js: 135 : 19) tại C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js: 32: 16 tại C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ waterline \ lib \ waterline \ core \ validations.js: 184: 23 khi hoàn tất (C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async. js: 135: 19) tại C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js: 32: 16 tại C: \ Sử dụng rs \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ waterline \ lib \ waterline \ core \ validations.js: 157: 64 tại C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js: 125: 13 tại Array.forEach (bản địa) tại _each (C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async. js: 46: 24) tại Object.async.each (C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js: 124: 9) xác thực (C : \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ waterline \ lib \ waterline \ core \ validations.js: 156: 11) tại C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js: 125: 13 Thuộc tính không hợp lệ được gửi tới Người dùng: • email • undefined phải là một thư điện tử (thay vì "admin @ gmailasd", mà là một chuỗi)

Trả lời

6

Cách đúng để khai báo một trường email là như thế này:

email: { 
    type: 'email', 
    required: true,//Email field will be required for insert or update 
    unique: true //Insert or update will crash if you try to insert duplicate email 
}, 

Bạn có thể xem tất cả loại attribut khác nhau ở đây http://sailsjs.org/documentation/concepts/models-and-orm/attributes

Nếu bạn muốn bắt chèn lỗi/cập nhật, bạn có thể làm điều này trên điều khiển của bạn:

MyModel.create({email:email}).exec(function(err, model) 
{ 
    if(err) 
    { 
     //Check if it's a validation error or a crash 
     if(err.code == "E_VALIDATION") 
     sails.log.debug("valid fail, check form"); 
     else 
     sails.log.debug("crash"); 
    } 
    else 
    { 
     //do what you want to do with the data 
    } 
}); 
+0

Nó vẫn đang nghiền: ( – ygrunin

+0

bạn có thể hiển thị nhật ký lỗi không? – jaumard

+1

Bình thường nếu bạn không chỉ định trên email người được yêu cầu xác nhận ném một ngoại lệ đó là bình thường – jaumard

2

Câu trả lời của cô ấy. Nhờ jaumard, tôi đã tìm thấy vấn đề. Tôi đã sử dụng trường không xác định do nhầm lẫn, mà không kiểm tra xem có tồn tại trước err.originalError.code nhưng không được xác định. Vì vậy, cách chính xác là: err.originalError & & err.originalError.code & & err.originalError.đang === 11000

và không err.originalError.code === 11000.

1

Các phiên bản trước của Sails khuyến cáo rằng xác nhận email đã đạt được như

email này: { loại: 'chuỗi ', email: đúng, yêu cầu: đúng },

phiên bản hiện tại nên như thế này

email: { loại: 'email', bắt buộc: true },

+0

Nộp [một vấn đề] (https://github.com/balderdashy/sails-docs/issues/588) cho tài liệu về thuộc tính 'email'. – Roy

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