2016-10-07 1 views
4

に失敗しました提出をクリックした後、登録フォームを使用してデータベースにデータを提出し、それはすぐにノード端子に表示されます。 ここnode terminalValidationErrorを:ユーザ検証は、私はMongoDBのを使用してパスポートローカル認証とシンプルなNode.jsのアプリを作成して、フレームワークとして表現しようとしていますが、私はしようとするたびに、私は問題</p> <p>を抱えているMongooseError.ValidationError

である私のユーザー・スキーマがどのように見えるか:

var mongoose = require('mongoose'); 
var bcrypt = require('bcrypt-nodejs'); 

// define the schema for our user model 
var userSchema = mongoose.Schema({ 

    local   : { 
     name : String, 
     username  : String, 
     mobile  : Number, 
     email  : String, 
     gender  : String, 
     password  : String 

    } 
}); 

// methods ====================== 
// generating a hash 
userSchema.methods.generateHash = function(password) { 
    return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); 
}; 

// checking if password is valid 
userSchema.methods.validPassword = function(password) { 
    return bcrypt.compareSync(password, this.local.password); 
}; 

// create the model for users and expose it to our app 
module.exports = mongoose.model('User', userSchema); 

と私のルータのファイル:

passport.use('local-signup', new LocalStrategy({ 

    nameField  : 'name',  
    usernameField : 'username', 
    mobileField : 'mobile', 
    emailField : 'email', 
    genderField : 'gender', 
    passwordField : 'password', 
    passReqToCallback : true // allows us to pass back the entire request to the callback 
}, 
function(req, name, username, mobile, email, gender, password, done) { 

    // asynchronous 
    // User.findOne wont fire unless data is sent back 
    process.nextTick(function() { 

    // find a user whose email is the same as the forms email 
    // we are checking to see if the user trying to login already exists 
    User.findOne({ 'local.email' : email }, function(err, user) { 
     // if there are any errors, return the error 
     if (err) 
      return done(err); 


     // check to see if theres already a user with that email 
     if (user) { 
      return done(null, false, req.flash('signupMessage', 'That email is already taken.')); 
     } else { 

      // if there is no user with that email 
      // create the user 
      var newUser   = new User(); 

      // set the user's local credentials 
      newUser.local.name  = name; 
      newUser.local.username = username; 
      newUser.local.mobile = mobile; 
      newUser.local.email = email; 
      newUser.local.gender = gender; 
      newUser.local.password = newUser.generateHash(password); 

      // save the user 
      newUser.save(function(err) { 
       if (err) 
        throw err; 

       return done(null, newUser); 
      }); 
     } 

    }); 

    }); 

})); 

私は
おかげ

答えて

6

をNode.jsのと同様のMongoDBのように私を助けてくださいに新しいです:サインアップロジックのための

// process the signup form 
    app.post('/signup', passport.authenticate('local-signup', { 
     successRedirect : '/profile', // redirect to the secure profile section 
     failureRedirect : '/signup', // redirect back to the signup page if there is an error 
     failureFlash : true // allow flash messages 
    })); 

パスポート構成理由:このエラーの原因は、dbに格納する無効なタイプです。モバイルのように数値型ですが、数値に変換できない値を渡している場合は、同じエラーが発生します。

console.log(newUser);ユーザーを保存して、移動フィールドに渡す値をチェックする前に、そのデータ型がスキーマの番号であるためNumberに変換可能です。

モバイルが "未定義またはnull、つまり番号に変換できない場合は動作しません。値が存在しない場合は、オブジェクトからこのキーを削除します。未定義、ヌル、または ""文字列(数値に変換できない)を渡さないでください。

+4

ありがとうございました – Zub

関連する問題