ハッシュされたパスワードを保存しようとすると問題が発生します。私はそれを保存していたときと同じように働いていましたが、今は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"}
パスワードを返す。私は間違って何をしていますか?
ありがとうございます。それは今働きます! –