【发布时间】:2016-04-19 19:23:23
【问题描述】:
我正在尝试使用 JavaScript-MD5 插件对用户名进行哈希处理,然后将其存储到数据库中以与 Jdenticon 一起使用。我可以使用var hash = md5($scope.username); 对密码进行哈希处理并将其记录到控制台,但无法将其传递给我的 newUser 变量。
-
注册控制器
$scope.register = function(){ var hash = md5($scope.username); console.log(hash); var newUser = { email: $scope.email, password: $scope.password, username: $scope.username, userHash: hash }; $http.post('/users/register', newUser).then(function(){ $scope.email = ''; $scope.password = ''; $scope.username = ''; userHash = ''; }; -
注册路线:
app.post('/users/register', function(req, res) { bcrypt.genSalt(10, function(err, salt) { bcrypt.hash(req.body.password, salt, function(err, hash) { var user = new User({ email: req.body.email, password: hash, username: req.body.username, userHash: req.body.userHash }); console.log(user); user.save(function(err) { if (err) return res.send(err); return res.send(); }); }); }); });
【问题讨论】:
-
您的客户端控制台中是否有任何错误?您的节点控制台记录什么?
-
req.body.userHash在服务器端显示什么值? -
没有显示 userHash 的值。登录测试用户:
{ created: Tue Apr 19 2016 15:37:25 GMT-0400 (Eastern Daylight Time), _id: 57168933dbe10f0c2860c180, username: 'green', password: '$2a$10$U4pqSpgjM74pqSpgjM7NwEj8BnR4I.uWlODNXyld2NHmj0iGBgcETawCCZkk0G', email: 'green@blue.com' } -
userHash存在于您的User模型中吗? -
不是,感谢您的帮助。
标签: javascript angularjs mongodb hash md5