2016-07-26 3 views
0

POSTで渡されたパラメータに関連するモデルの有効なIDが含まれているかどうかを確認するために、モデル内でvalidate()を使用しようとしています。ここで私は私のPerson.jsファイル内に記述されたコードは次のとおりです。ループバックカスタムのバリデーション

'use strict'; 

module.exports = function(Person) { 
    Person.validate('countryId', function(err) { 
    var Country = Person.app.models.Country; 
    var countryId = this.countryId; 
    if (!countryId || countryId === '') 
     err(); 
    else { 
     Country.findOne({where: {id: countryId}}, function(error, result) { 
     if (error || !result) { 
      err(); 
     }; 
     }); 
    }; 
    }); 
}; 

人Userモデルの子孫であると私は(例えば、重複する電子メールのような)他のエラーのあるフィールドでモデルを作成しようとすると、その応答は、エラーが含まれています私の小切手に関連するメッセージ。私は何か間違っているのですか?これはバグですか?

答えて

0

は、次の例のように、非同期検証を使用する必要があります。

'use strict'; 

module.exports = function(Person) { 

    Person.validateAsync('countryId', countryId, { 
     code: 'notFound.relatedInstance', 
     message: 'related instance not found' 
    }); 

    function countryId(err, next) { 
     // Use the next if countryId is not required 
     if (!this.countryId) { 
      return next(); 
     } 

     var Country = Person.app.models.Country; 

     Country.exists(this.countryId, function (error, instance) { 
      if (error || !instance) { 
       err(); 
      } 
      next(); 
     }); 
    } 

}; 
関連する問題