2015-07-27 12 views
10

Próbuję zapisać nowy dokument w mongodb z mangustą, ale dostaję ValidationError: Path 'email' is required., Path 'passwordHash' is required., Path 'username' is required. mimo że dostarczam email, passwordHash i nazwę użytkownika.Mongoose: wymagana ścieżka błędu sprawdzania poprawności

Oto schemat użytkownika.

var userSchema = new schema({ 
     _id: Number, 
     username: { type: String, required: true, unique: true }, 
     passwordHash: { type: String, required: true }, 
     email: { type: String, required: true }, 
     admin: Boolean, 
     createdAt: Date, 
     updatedAt: Date, 
     accountType: String 
    }); 

W ten sposób tworzę i zapisuję obiekt użytkownika.

var newUser = new user({ 

     /* We will set the username, email and password field to null because they will be set later. */ 
     username: null, 
     passwordHash: null, 
     email: null, 
     admin: false 

    }, { _id: false }); 

    /* Save the new user. */ 
    newUser.save(function(err) { 
    if(err) { 
     console.log("Can't create new user: %s", err); 

    } else { 
    /* We succesfully saved the new user, so let's send back the user id. */ 

    } 
    }); 

Więc dlaczego mangusta zwróci błąd sprawdzania poprawności, nie mogę używać null wartość jako tymczasowy?

+1

Cóż, nie wiem, gdzie ustawiasz adres e-mail newUsers, passwordHash i nazwę użytkownika. ustawiasz je na wartość null, a następnie próbujesz zapisać. –

+0

Nie możesz tego zrobić? 'null' jest wartością. – 2trill2spill

Odpowiedz

9

W odpowiedzi na twój ostatni komentarz.

Masz rację, że wartość pusta jest typem wartości, ale typy zerowe są sposobem informowania interpretera, że ​​ma on wartość bez wartości. dlatego musisz ustawić wartości na dowolną wartość inną niż null lub otrzymasz błąd. w twoim przypadku ustaw te wartości na puste ciągi. tj.

var newUser = new user({ 

    /* We will set the username, email and password field to null because they will be set later. */ 
    username: '', 
    passwordHash: '', 
    email: '', 
    admin: false 

}, { _id: false }); 
1

Cóż, w następujący sposób pozbyłem się błędów. Miałem następujący schemat:

var userSchema = new Schema({ 
    name: { 
     type: String, 
     required: 'Please enter your name', 
     trim: true 
    }, 
    email: { 
     type: String, 
     unique:true, 
     required: 'Please enter your email', 
     trim: true, 
     lowercase:true, 
     validate: [{ validator: value => isEmail(value), msg: 'Invalid email.' }] 
    }, 
    password: { 
     type: String/ 
     required: true 
    }, 
    // gender: { 
    //  type: String 
    // }, 
    resetPasswordToken:String, 
    resetPasswordExpires:Date, 
}); 

i mój zacisk wyrzucić mnie następujące dziennika, a następnie udał się do nieskończonej reload na nazywając moją funkcję register:

(node:6676) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ValidationError: password: Path password is required., email: Invalid email.

(node:6676) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Tak, jak to powiedział Path „Hasło” jest wymagane, skomentowałem linię required:true z mojego modelu i linię validate:email z mojego modelu.

Powiązane problemy