2017-06-14 20 views
1

這裏是我的User模型定義的NodeJS Sequelize ORM model.validation不是一個函數

options: { 
     tableName: 'cushbu_users', 
     hooks:{}, 
     timestamps:false, 

     /*--field names and validation rules --*/ 
     email:{ 
      type:Sequelize.STRING, 
      validate:{ 
       notEmpty:true 
      } 
     }, 
     first_name:{ 
      type:Sequelize.STRING, 
      validate:{ 
       notEmpty:true 
      } 
     } 
    } 

我怎樣才能得到模型驗證錯誤消息在我的控制器

我與這些

User.validate().success(function() { 
      console.log('ok'); 
     }).error(function (error) { 
      console.log(error); 
     }) 
嘗試

但是我得到以下錯誤user.validate not function

+0

我也一樣。你有沒有解決這個問題@Jabaa –

回答

0

您可以在Model類中添加classMethods,並將邏輯放入此模型並返回錯誤。從喜歡User.validator()路線

classMethods: { 
     validator: function() { 
     var errors = []; 
     if (!this.email){ 
      errors.push('Email id should not be null'); 
     } 
     . 
     . 
     . 
     . 
     . 
     return errors; 
     }, 
} 

調用方法,如果返回空數組那麼你User對象是有效的其他顯示錯誤消息。

+0

但我想模型驗證 – Jabaa

+0

請參閱 - http://docs.sequelizejs.com/manual/tutorial/models-definition.html#validators-and-allownull- –

0

你應該叫從即時對象這種方法不是sequelize模式是這樣的:

User.create({ 
     name: "test" 
     email: "test" 
     password: "test" 
    }).then(function(user){ 
     // validate 
     errors = user.validate(); 

    }); 
+0

我已經試過這個但返回相同的錯誤 – Jabaa

+0

我編輯了我的答案@Jabaa – farhadamjady

+0

讓我檢查它 – Jabaa

相關問題