2016-04-19 13 views
1

JavaScript-MD5プラグインを使用してユーザー名をハッシュし、データベースに保存してJdenticonと共に使用しようとしています。パスワードをハッシュしてvar hash = md5($scope.username);を使用してコンソールにログすることはできますが、これをnewUser変数に渡すことはできません。データベースにハッシュされたユーザー名を保存しようとしています

  1. 合わせコントローラ

    $scope.register = function(){ 
    var hash = md5($scope.username); 
    console.log(hash); 
    var newUser = { 
        email: $scope.email, 
        password: $scope.password, 
        username: $scope.username, 
        userHash: hash 
    }; 
    
    $http.post('/users/register', newUser).then(function(){ 
        $scope.email = ''; 
        $scope.password = ''; 
        $scope.username = ''; 
        userHash = ''; 
    }; 
    
  2. 登録ルート:

    app.post('/users/register', function(req, res) { 
    
        bcrypt.genSalt(10, function(err, salt) { 
        bcrypt.hash(req.body.password, salt, function(err, hash) { 
         var user = new User({ 
         email: req.body.email, 
         password: hash, 
         username: req.body.username, 
         userHash: req.body.userHash 
         }); 
         console.log(user); 
    
         user.save(function(err) { 
         if (err) return res.send(err); 
    
         return res.send(); 
         }); 
        }); 
        }); 
    }); 
    
+2

'req.body.userHash'用サーバ側に示されているどのような値? –

+0

userHashの値は表示されません。ログに記録されたテストユーザー: '{作成:火曜2016年4月19日午前15時37分25秒GMT-0400(東部夏時間)、 _id:57168933dbe10f0c2860c180、 ユーザ名: 'グリーン'、 パスワード: '$ 2A $ 10 $ U4pqSpgjM74pqSpgjM7NwEj8BnR4I.uWlODNXyld2NHmj0iGBgcETawCCZkk0G' 、 email: '[email protected]'} ' –

+1

' user'モデルに 'userHash'がありますか? –

答えて

1

私はあなたがuserHash内に格納することはできません理由ですあなたのUserモデルでuserHashプロパティを逃したことが推測あなたのデータベース。

Userモデルに最初にuserHashを含めて、うまくいくはずです。

のような:

var mongoose = require('mongoose'), 
    Schema = mongoose.Schema; 

var UserSchema= new Schema({ 
    username : { 
     type: String, 
     required: true 
    }, 
    email: { 
     type: String 
    }, 
    password: { 
     type: String 
    }, 
    userHash:{ 
     type: String 
    } 
}); 
mongoose.model('User', UserSchema); 
関連する問題