Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDaniel Conde
3,881 PointsDealing with error in bcrypt.compare callback
Is there any reason why we don't pass a potential error passed back by the bcrypt function? Would adding the following line help? if(error) return callback(error); How do you recommend dealing with potential internal errors such as this one?
The code I'm referring to:
bcrypt.compare(password, user.password, function(error, result) { if (result === true) { return callback(null, user); } else { return callback() } })
2 Answers
Christos Peramatzis
16,428 PointsI would expect it to be
bcrypt.compare(password, user.password, function(error, result) {
if(result === true) {
return callback(null, user);
}
else {
return callback(error);
}
}
We don't need to differentiate between having an error or the passwords not matching, maybe except for the status codes.
In any case, I don't know why the error is not passed in the callback in the video.
Kayla Kremer
14,269 PointsI think the reason why the error is not passed in the callback is because passing parameters in JavaScript is optional. So by not passing in anything, the error and result parameters become undefined.
If you notice in the video in the index.js file, we set up the if conditional to check if there is an error or if there is no user:
if(error || !user){
var err = new Error('Wrong email or password.');
err.status = 401;
return next(err);
}
Since both error and user are undefined, error evaluates to false, but !user evaluates to true, so the if conditional statement is true and the block of code runs.
Oziel Perez
61,321 PointsWhen one knows more than one or two programming languages, that can really trick your head! Some languages require that you pass something, even if it's a null value, or it will throw an error. I think I rather pass something in to specifically state that the values are null