试图将哈希用户名存储在数据库中
Trying to store hashed username in database
我正在尝试使用 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();
});
});
});
});
我想您可能在 User
模型中遗漏了 userHash
属性,这就是为什么您无法将 userHash
存储在数据库中的原因。
所以你应该首先在你的 User
模型中包含 userHash
然后应该可以正常工作。
喜欢:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var UserSchema= new Schema({
username : {
type: String,
required: true
},
email: {
type: String
},
password: {
type: String
},
userHash:{
type: String
}
});
mongoose.model('User', UserSchema);
我正在尝试使用 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(); }); }); }); });
我想您可能在 User
模型中遗漏了 userHash
属性,这就是为什么您无法将 userHash
存储在数据库中的原因。
所以你应该首先在你的 User
模型中包含 userHash
然后应该可以正常工作。
喜欢:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var UserSchema= new Schema({
username : {
type: String,
required: true
},
email: {
type: String
},
password: {
type: String
},
userHash:{
type: String
}
});
mongoose.model('User', UserSchema);