0
My APIはSailsJsとMongoDbを使用します。ユーザーの作成のために、beforeCreate lifecycle callback
を使用してパスワードフィールドをハッシュするためにbcryptを実行しています。Sails Mongoブロックでパスワードフィールドを 'required'に設定するbeforeCreate
私は
{required : true}
に私のencrPasswordフィールドを設定した場合、私のコードは動作を停止し、次のエラーを放射する私は、この奇妙エラーが発生します。ここ
{
"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();
});
});
}
ps。私は厳密にタイプミスをチェックした。上記のサンプルのミスや構文エラーは、共有したくないコードを非表示にするための編集結果です。