I am getting a bcrypt error stating that data and hash arguments are required, referencing line #44 in my routes.js file. From what I can tell, I am passing that information: the first parameter to bcrypt.compare is the user entered password, and the second is the hashed password retrieved from the db. What am I doing wrong?
JavaScript
x
2
1
bcrypt.compare(req.params.password, user.password, function
2
routes.js
JavaScript
1
53
53
1
'use strict'
2
3
var express = require('express');
4
var router = express.Router();
5
var User = require('../app/models/user');
6
//password hashing
7
var bcrypt = require('bcrypt');
8
9
var count = 0;
10
11
router.use(function(req, res, next) {
12
count++;
13
console.log('API hit count = %s', count);
14
next();
15
});
16
17
// /users post(create new user) get(specific user)
18
router.route('/users')
19
.post(function(req,res) {
20
var user = new User();
21
user.username = req.body.username;
22
user.password = bcrypt.hashSync(req.body.password, 10);
23
24
//save the user and checkfor errors
25
user.save(function(err) {
26
if (err) {
27
res.send(err);
28
} else {
29
res.json({message: "User created!"});
30
}
31
});
32
33
})
34
35
router.route('/users/:username')
36
.get(function(req, res) {
37
var query = {
38
username: req.params.username,
39
};
40
User.findOne(query, function(err, user) {
41
if (err) {
42
res.send(err);
43
} else {
44
bcrypt.compare(req.params.password, user.password, function(err, res) {
45
if(err) {
46
console.log('Comparison error: ', err);
47
}
48
})
49
res.json(user);
50
}
51
});
52
})
53
Advertisement
Answer
bcrypt.compare
takes 3 parameters; passwordToCheck, passwordHash, and a callback, respectively. (Check the documentation for examples)
This error means one or both of the first 2 parameters are either null or undefined. Therefore, make sure both of them are passed correctly. (Not as null or undefined)