Strongloop环回验证和请求生命周期

Strongloop loopback validation and request lifecycle

本文关键字:请求 生命 周期 验证 Strongloop      更新时间:2023-09-26

我想在模型保存到数据库之前更新它,在验证发生之后。

在环回请求生命周期中的正确点是什么(哦,这开始让我想起。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
}

理想情况下,我希望在调用addCreatorIdsendToThirdParty函数之前触发默认的环回模型验证。我该怎么做呢?

我可以在我的before save钩子中清楚地调用model.isValid(),但似乎我应该能够重新排列这些,以便自动发生。

环回操作钩子文档没有提到验证何时发生,远程钩子文档也没有。

"我想在模型保存到数据库之前更新它,在验证发生之后。"我有解。在环回中使用'persist'钩子,它在验证之后和保存数据到db之前被调用。您可以使用它的"ctx"插入或更改任何数据。数据的参数。希望它能有所帮助,虽然有点晚了!链接:https://loopback.io/doc/en/lb3/Operation-hooks.html坚持