Swa*_*kar 5 node.js strongloop loopbackjs
使用 Loopback 框架,我想在Item编辑之前执行一些操作,因此我正在尝试此操作但无法将其绑定到更新挂钩。
Item.beforeRemote("update", function(ctx,myitem,next) {
console.log("inside update");
});
Run Code Online (Sandbox Code Playgroud)
相反,更新我与尝试updateAttributes,updateById,创建但没有工作。这种 beforeRemote 钩子适用于 create on POST,但在编辑期间无法通过PUT获取它。留给我的最后一个解决方案是再次检查带有通配符钩子的 methodString,但我想知道是否有任何我找不到的文档记录。
Item.beforeRemote("**",function(ctx,instance,next){
console.log("inside update");
});
Run Code Online (Sandbox Code Playgroud)
我知道自这篇文章开放以来已经过去了两年,但是如果任何机构有相同的问题并且如果您使用端点,your_model/{id}则 afterRemote 钩子是replaceById. 如果您需要知道在远程钩子中触发了哪个方法,请使用以下代码:
yourModel.beforeRemote('**', function(ctx, unused, next) {
console.info('Method name: ', ctx.method.name);
next();
});
Run Code Online (Sandbox Code Playgroud)
与评论相反,save是一个远程钩子,不是操作钩子,但你想把它用作:prototype.save。相关的操作挂钩将是before save. 您可以在LoopBack 文档页面上看到这些表格。不过,我可能会将其实现为一个操作挂钩,并使用isNewInstance上下文中的属性仅在更新时执行操作:
Item.observe('before save', function(ctx, next) {
if (ctx.isNewInstance) {
// do something with ctx.currentInstance
}
next();
});
Run Code Online (Sandbox Code Playgroud)