2015-04-16 18 views
5

mentre la regola di convalida della posta elettronica non riesce sul modulo di sails.js, il server si arresta in modo anomalo. Qui il frammento di mio modulo: Indirizzo e-mail // dell'utente"email" regola di convalida arresto anomalo server - Mongo con Sails.js

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

E l'errore come di seguito:

err: Errore (E_VALIDATION) :: 1 attributo non è valido a WLValidationError. WLError (C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ waterline \ lib \ waterline \ error \ WLError.js: 26: 15) al nuovo WLValidationError (C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ waterline \ lib \ waterline \ error \ WLValidationError.js: 20: 28) in 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) completato (C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js: 135 : 19) in C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js: 32: 16 in C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ waterline \ lib \ waterline \ core \ validations.js: 184: 23 completato (C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async. js: 135: 19) in C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js: 32: 16 in C: \ Use rs \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ waterline \ lib \ waterline \ core \ validations.js: 157: 64 in C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js: 125: 13 in Array.forEach (nativo) in _each (C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async. js: 46: 24) su Object.async.each (C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js: 124: 9) alla validazione (C : \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ waterline \ lib \ waterline \ core \ validations.js: 156: 11) in C: \ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js: 125: 13 Attributi non validi inviati all'utente: • email • undefined dovrebbe essere un e-mail (al posto di "admin @ gmailasd", che è una stringa)

risposta

6

Il modo corretto per dichiarare un campo di e-mail è come questo:

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 
}, 

si può vedere tutto diversi tipi attribut qui http://sailsjs.org/documentation/concepts/models-and-orm/attributes

Se si desidera catturare inserto errori/aggiornamento si può fare questo sul controller:

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

Si sta schiacciando ancora :( – ygrunin

+0

può mostrare il log degli errori? – jaumard

+1

Normale se non si specifica sull'e-mail chi è richiesta la validazione lanciare un'eccezione è normale – jaumard

2

Lei la risposta. Grazie a jaumard, ho trovato il problema. Ho usato un campo non definito per errore, senza verificare se esiste prima err.originalError.code ma non era definito. Quindi il modo corretto è: err.originalError & & err.originalError.code & & err.originalError.Codice === 11000

e non err.originalError.code === 11000.

1

Le versioni precedenti di vele raccomandato che la convalida e-mail è stato raggiunto in questo modo

email: { Tipo: 'stringa ', email: vero, required: true} ,

La versione attuale dovrebbe essere come questo

e-mail: { Tipo: 'e-mail', richiesto: true} ,

+0

Archiviato [un problema] (https://github.com/balderdashy/sails-docs/issues/588) per la documentazione sull'attributo 'email'. – Roy

Problemi correlati