我使用在用户模型中创建的水手
module.exports = {
attributes: {
login: {
type: 'string',
},
password: {
type: 'string',
},
birthDate: {
type: 'date',
}
},
beforeCreate: function(values, next) {
//someCode();
},
beforeUpdate: function(values, next) {
console.log("i\'m here!");
if (values.birthDate && _.isNaN(Date.parse(values.birthDate))) {
console.log(values.birdDate);
return next({err: ["Wrong date!"]});
}
values.birthDate = Date.parse(values.birthDate);
next();
}
};我希望在beforeUpdate方法中更改日期值的字符串值,但不调用此方法。可能是我必须亲自打电话吗?
发布于 2015-02-22 22:15:53
您的beforeUpdate方法从不执行,因为您有验证错误。
birthDate应该是日期,但sails会得到字符串并停止更新过程。
因此,您应该在验证模型的记录之前将其转换到日期(并且在更新之前尝试这样做);
只需将beforeUpdate方法重命名为beforeValidate即可。这可能会有帮助。
您可以找到sails执行模型的生命周期方法这里的顺序。
https://stackoverflow.com/questions/28662395
复制相似问题