我想知道在Sails框架中触发beforeValidate时的当前操作,请查看我的代码:
module.exports = {
attributes: {
...
log: 'string'
},
beforeValidate: function(values, cb) {
// I want to know the action is create new record or update current record
// If action is update, how to get primary key of record will be updated
/*
if (isCreateNewRecord) {
values.log = 'New created...'; // for example
} else {
values.log = 'Update record ID: ' + recordID;
}
*/
cb();
}
}
非常感谢你的帮助!
答案 0 :(得分:2)
嗯,你不应该这样做......这就是为什么我们有beforeCreate
和beforeUpdate
!
beforeCreate: function (values, cb) {
values.log = 'New created...';
return cb();
},
beforeUpdate: function (valuesToUpdate, cb) {
valuesToUpdate.log = 'Update record ID: ' + valuesToUpdate.id;
return cb();
}
查看the docs。