Reputation: 25
I want to know current action when trigger beforeValidate in Sails framework, please review my code:
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();
}
}
Thanks so much for help!
Upvotes: 1
Views: 517
Reputation: 3114
Well, you're not supposed to do that... And this is why we have beforeCreate
and beforeUpdate
!
beforeCreate: function (values, cb) {
values.log = 'New created...';
return cb();
},
beforeUpdate: function (valuesToUpdate, cb) {
valuesToUpdate.log = 'Update record ID: ' + valuesToUpdate.id;
return cb();
}
Check out the docs.
Upvotes: 2