I want to update a model before it's saved to the database, after validation has occurred.
What is the correct point in the loopback request lifecycle (uh oh, this is starting to remind me of .NET webforms!) to do this?
Report.validatesPresenceOf('basicInfo');
Report.beforeRemote('create', addCreatorId);
function addCreatorId(ctx, instance, next) {
// alter the model, validation has not occurred yet
}
Report.observe('before save', sendToThirdParty);
function sendToThirdParty(ctx, instance, next) {
// send contents to third party, alter model with response
// validation has not occurred yet
}
Report.afterRemote('create', sendEmail);
function sendEmail(ctx, record, next) {
// model has been saved to the database
// validation occurs before this point
}
Ideally I want the default loopback model validation to trigger before the addCreatorId
and sendToThirdParty
functions are called. How should I go about doing that?
I could clearly call model.isValid()
in my before save
hook, but it seems like I should be able to rearrange these so that happens automatically.
The loopback Operation Hooks documentation doesn't mention when validation occurs, nor do the Remote Hooks docs.
"I want to update a model before it's saved to the database, after validation has occurred." I have solution for this. Use 'persist' hook in loopback which is called after validation and before saving data to db. You can insert or change any data you want using its 'ctx.data' parameter. Hope it helps, a bit late, though! Link: https://loopback.io/doc/en/lb3/Operation-hooks.html#persist