sequelize.js custom validator, check for unique username / password

后端 未结 4 722
野趣味
野趣味 2021-02-02 03:47

Imagine I have defined the following custom validator function:

isUnique: function () { // This works as expected
  throw new Error({error:[{message:\'Email addr         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-02 04:16

    Success callback is called even if no user is found. You have to check if the function passes a user as an argument:

    isUnique: function (email) { // This doesn't work
      var User = seqeulize.import('/path/to/user/model');
    
      User.find({where:{email: email}})
        .success(function (u) { // This gets called
          if(u){
            throw new Error({error:[{message:'Email address already in use!'}]});  // But this isn't triggering a validation error.
          }
        });
    }
    

提交回复
热议问题