Strongloop 环回验证和请求生命周期
Strongloop loopback validation and request lifecycle
我想在模型保存到数据库之前更新模型,验证发生后。
环回请求生命周期中的正确点是什么(呃哦,这让我想起了 .NET webforms!)来做到这一点?
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
}
理想情况下,我希望在调用 addCreatorId
和 sendToThirdParty
函数之前触发默认环回模型验证。我应该怎么做?
我可以清楚地在我的 before save
挂钩中调用 model.isValid()
,但似乎我应该能够重新排列它们以便自动发生。
环回 Operation Hooks documentation doesn't mention when validation occurs, nor do the Remote Hooks 文档。
"I want to update a model before it's saved to the database, after validation has occurred."
我有解决方案。在验证之后和将数据保存到数据库之前调用的环回中使用 'persist' 挂钩。您可以使用其 'ctx.data' 参数插入或更改您想要的任何数据。希望它有所帮助,虽然有点晚了!
Link: https://loopback.io/doc/en/lb3/Operation-hooks.html#persist
我想在模型保存到数据库之前更新模型,验证发生后。
环回请求生命周期中的正确点是什么(呃哦,这让我想起了 .NET webforms!)来做到这一点?
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
}
理想情况下,我希望在调用 addCreatorId
和 sendToThirdParty
函数之前触发默认环回模型验证。我应该怎么做?
我可以清楚地在我的 before save
挂钩中调用 model.isValid()
,但似乎我应该能够重新排列它们以便自动发生。
环回 Operation Hooks documentation doesn't mention when validation occurs, nor do the Remote Hooks 文档。
"I want to update a model before it's saved to the database, after validation has occurred." 我有解决方案。在验证之后和将数据保存到数据库之前调用的环回中使用 'persist' 挂钩。您可以使用其 'ctx.data' 参数插入或更改您想要的任何数据。希望它有所帮助,虽然有点晚了! Link: https://loopback.io/doc/en/lb3/Operation-hooks.html#persist