【发布时间】:2017-02-16 18:15:14
【问题描述】:
我想在模型保存到数据库之前更新模型,在验证发生之后。
环回请求生命周期中的正确点是什么(哦哦,这开始让我想起 .NET 网络表单了!)这样做?
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 文档没有提及验证发生的时间,Remote Hooks 文档也没有提及。
【问题讨论】:
标签: javascript node.js validation loopbackjs strongloop