Node.js 将密码字段设置为';必需的';在航行中,在创建之前使用Mongo块
我的API使用SailsJs和MongoDb。对于用户创建,我使用Node.js 将密码字段设置为';必需的';在航行中,在创建之前使用Mongo块,node.js,sails.js,waterline,bcrypt,sails-mongo,Node.js,Sails.js,Waterline,Bcrypt,Sails Mongo,我的API使用SailsJs和MongoDb。对于用户创建,我使用beforeCreate生命周期回调运行bcrypt来散列我的密码字段 我遇到这个wierd错误,如果我将encrPassword字段设置为 {required : true} 我的代码停止工作并发出以下错误: { "error": "E_VALIDATION", "status": 400, "summary": "1 attribute is invalid", "model": "User", "in
beforeCreate生命周期回调
运行bcrypt来散列我的密码字段
我遇到这个wierd错误,如果我将encrPassword字段设置为
{required : true}
我的代码停止工作并发出以下错误:
{
"error": "E_VALIDATION",
"status": 400,
"summary": "1 attribute is invalid",
"model": "User",
"invalidAttributes": {
"encrPassword": [
{
"rule": "string",
"message": "Value should be a string (instead of null, which is an object)"
},
{
"rule": "required",
"message": "\"required\" validation rule failed for input: null\nSpecifically, it threw an error. Details:\n undefined"
}
]
}
以下是我的代码示例:
attributes: {
fullname : {type : 'string'},
username : {type : 'string', unique:true, required:true},
encrPassword : {type : 'string'}, // ==> this works
// encrPassword : {type : 'string', required:true}, ==> this doesn't
},
insert : function(req, cb){
console.log('Insert ', req);
if(typeof req.fullName == 'string' && typeof req.username == 'string'){
User.findOne({username : req.username}).exec(function(err, res){
if(!res){
User.create(req).exec(function(err, resp){
console.log('create', null, resp);
if(err)
cb(err);
else cb(null, resp);
});
}
else cb({message: 'already eists'})
});
}
else cb({message: 'Bad Request'});
},
beforeCreate : function(req, next){
console.log('In bcrypt');
bcrypt.genSalt(10, function(err, salt){
if(err) return next(err);
bcrypt.hash(req.password, salt, function(err, hash){
if(err) return next(err);
req.encrPassword = hash;
delete req.password;
console.log('Leaving BCrypt');
next();
});
});
}
我严格检查打字错误;上述示例中的任何打字错误或语法错误都是编辑以隐藏我不想共享的任何代码的结果在调用
beforeCreate
此时没有
encrPassword
,因此出现错误