2016-08-31 5 views
0

ハッシュされたパスワードを保存しようとすると問題が発生します。私はそれを保存していたときと同じように働いていましたが、今はbcryptを実装したのでもう動作しません。返されたJSONにはパスワードも含まれていません。ここで 生成後にパスワードを保存したくない

は私のコードです:

Userモデル:

49 userSchema.methods.registerUser = function(page, body, cb) { 
50 if (page == 1) { 
51  this.username = body.username; 
52  this.email = body.email; 
53  this.generatePassword(body.password, function(err, hash) { 
54  if (err) cb(err); 
55  this.password = hash; 
56  }); 
57 } else if (page == 2) { 
58  this.firstName = body.firstName; 
59  this.lastName = body.lastName; 
60  this.gender = body.gender; 
61  this.dateOfBirth = this.createDate(body.day, body.month, body.year); 
62  this.age = this.calculateAge(this.dateOfBirth); 
63 } else { 
64  cb("ERR! Page " + page + " doesnt exist"); 
65 } 
66 
67 cb(); 
68 }; 

84 userSchema.methods.generatePassword = function(password, cb) { 
85 bcrypt.hash(password, null, null, function(err, hash) { 
86  cb(err, hash); 
87 }) 
88 }; 

ユーザコントローラ:

11 router.post('/register/:page', function(req, res) { 
12 user.registerUser(req.params.page, req.body, function() { 
13  if (req.params.page == 1) res.redirect('/register/2'); 
14  if (req.params.page == 2) res.json(user); 
15 }); 
16 }); 

返さJSON:あなたはそれはdoesnの見ることができるように

{"email":"[email protected]","username":"ivanerlic","age":21,"dateOfBirth":"1994-09-01T05:58:43.204Z","gender":"male","lastName":"Erlic","firstName":"Ivan","_id":"57c670438b0d398f07371386"} 

パスワードを返す。私は間違って何をしていますか?

答えて

1

thisが矛盾しているためです。

userSchema.methods.registerUser = function(page, body, cb) { 
50 if (page == 1) { 
51  this.username = body.username; 
52  this.email = body.email; 
     var self = this; 
53  this.generatePassword(body.password, function(err, hash) { 
54  if (err) return cb(err); 
55  self.password = hash; 
56  }); 
57 } else if (page == 2) { 
58  this.firstName = body.firstName; 
59  this.lastName = body.lastName; 
60  this.gender = body.gender; 
61  this.dateOfBirth = this.createDate(body.day, body.month, body.year); 
62  this.age = this.calculateAge(this.dateOfBirth); 
63 } else { 
64  cb("ERR! Page " + page + " doesnt exist"); 
65 } 
66 
67 cb(); 
68 }; 
+0

ありがとうございます。それは今働きます! –

0

ハッシュが実行される前にコールバックを呼び出しています。 正しいコードは次のようになります。

49 userSchema.methods.registerUser = function(page, body, cb) { 
 
50 if (page == 1) { 
 
51  this.username = body.username; 
 
52  this.email = body.email; 
 
53  this.generatePassword(body.password, function(err, hash) { 
 
54  if (err) cb(err); 
 
55  this.password = hash; 
 
     cb(); 
 
56  }); 
 
57 } else if (page == 2) { 
 
58  this.firstName = body.firstName; 
 
59  this.lastName = body.lastName; 
 
60  this.gender = body.gender; 
 
61  this.dateOfBirth = this.createDate(body.day, body.month, body.year); 
 
62  this.age = this.calculateAge(this.dateOfBirth); 
 
     cb(); 
 
63 } else { 
 
64  cb("ERR! Page " + page + " doesnt exist"); 
 
65 } 
 
68 };

それはまだ他の回答のようselfを使用して動作しない場合。

+0

まだ動作しませんでしたが、自己はしました。 –

+0

もちろん、コールバックも正しく使用する必要があります。このようなコードにIOが含まれていると、動作しません。 –

+0

うん、私はそれを実装しました。ありがとうございました! –

関連する問題