gpt4 book ai didi

validation - Sequelize 验证方法

转载 作者:行者123 更新时间:2023-12-04 15:44:07 27 4
gpt4 key购买 nike

我正在尝试验证我的模型,但我错过了一些东西,我不知道它是什么。
这是我的模块和他对电子邮件的验证。

module.exports = function(sequelize, DataTypes){
return sequelize.define("Scanner",
{
id : {
primaryKey : true,
autoIncrement : true,
type : DataTypes.INTEGER
},
email : {
type : DataTypes.STRING,
isUnique :true,
allowNull:false,
validate:{
isEmail : true
}
},
pin : {
type : DataTypes.INTEGER
}
},{
tableName : 'scanner'
});
};

当我尝试查找带有参数(pin + email)的对象时,如果我输入 this.email = ssdf.sdf ,我的查询就会启动,我想先检查我的参数是否正确。
Scanner.prototype.getScannerByCredentials = function(callback){
//Send only Field id and email
_Scanner.find({ where: { email : this.email, pin :this.pin},attributes:['id','email'] }).success(function(scanner) {
return callback(null, scanner);
}).error(function(error){
console.log(error);
return callback(error, null);
});
};

我尝试使用 validate() 方法,但出现错误: Object [object Object] has no method 'validate' 并且当我创建了 console.log(_Scanner);我看到了我的函数 validate() 所以我不知道为什么这不起作用..
Scanner.prototype.getScannerByCredentials = function(callback){
//Send only Field id and email
_Scanner.find({ where: { email : this.email, pin :this.pin},attributes:['id','email'] }).validate().success(function(scanner) {
return callback(null, scanner);
}).error(function(error){
console.log(error);
return callback(error, null);
});
};

我正在阅读文档并尝试了很多在网上建立的东西,所以如果有人可以向我解释出了什么问题,那就太好了。
提前非常感谢。

编辑:找到!

如果您有兴趣,我的解决方案:)

我的模型:
module.exports = function(sequelize, DataTypes){
return sequelize.define("Scanner",
{
id : {
primaryKey : true,
autoIncrement : true,
type : DataTypes.INTEGER,
allowNull : false
},
email : {
type : DataTypes.STRING,
isUnique :true,
allowNull:false,
validate:{
isEmail : true
}
},
pin : {
type : DataTypes.INTEGER
}
},{
tableName : 'scanner',
classMethods:{
isValid : function(objScanner) {
return this.build(objScanner).validate() ? true : false ;
}
}
});
};

我的凭据方法:
Scanner.prototype.getScannerByCredentials = function(callback){


//MODIF THIS PART / NEED TO IMPROVE
var error = _Scanner.build({ email : this.email, pin : this.pin}).validate();

if(error === null){
_Scanner.find({ where: { email : this.email, pin :this.pin},attributes:['id','email'] }).success(function(scanner) {
console.log(scanner);
return callback(null, scanner);
}).error(function(error){
console.log(error);
return callback(error, null);
});
}
else
return callback(error,null);
};

还有我的通用更新方法(奖金):
Scanner.prototype.update= function(callback){
var self = this;
_Scanner.find(this.id).success(function(scannerFound){
if(scannerFound){
//Set old Values by New Values : only values changed
scannerFound.dataValues = ormize(self,scannerFound.dataValues);

//Check validation Fields before insert DB
if(_Scanner.isValid(scannerFound)){
scannerFound.save().success(function(){
return callback(true);
}).error(function(error){
return callback(false);
});
}else
return callback(false);
}else
return callback(false);
}).error(function(error){
console.log(error);
return callback(false);
});
};

如果您对我的代码有任何建议,也将不胜感激:)
非常感谢提前

最佳答案

你用什么版本?这应该是 fixed

该字段应如下所示。

isEmail: {msg: 'Reason'}

关于validation - Sequelize 验证方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25278955/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com