Reputation: 391
I have a problem when I try to save a hashed password. When I used to save it just the way it was it worked but now that I implemented bcrypt it doesn't work anymore. The JSON returned doesn't even contain a password.
Here is my code:
User model:
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 };
User controller:
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 });
Returned JSON:
{"email":"[email protected]","username":"ivanerlic","age":21,"dateOfBirth":"1994-09-01T05:58:43.204Z","gender":"male","lastName":"Erlic","firstName":"Ivan","_id":"57c670438b0d398f07371386"}
As you can see it doesn't return a password. What am I doing wrong?
Upvotes: 0
Views: 33
Reputation: 13286
You are calling the callback before the hash is executed. The correct code should be:
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 };
If it still doesn't work use the self
as in the other answer.
Upvotes: 0
Reputation: 9406
This is becuase of conflicting 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 };
Upvotes: 1