保存后环回更新模型
Loopback update model after save
我想更新 "after save" 挂钩中的最后一个 inserted/updated 文档(行),而不创建像这样的新实例:
Model.observe('after save', function (ctx, next) {
ctx.someProperty = 'Foo';
ctx.update();
});
怎么可能?
我不确定你所说的 'update' 模型是什么意思。据我所知,通用模型 class 上没有 update()
功能。如果您正在寻找 updateAttribute
,那么有关该功能的文档是 here.
但是,假设您的问题只是 "How do I access the observed model inside of a loopback hook?",那么答案是实例存储在 ctx.instance
而不是作为 ctx
变量本身返回。请参阅示例 here。
例如
Model.observe('after save', function (ctx, next) {
ctx.instance.updateAttributes({someProperty: 'Foo'})
});
如果您可以更详细地描述您尝试使用 update()
函数实现的功能,我将尝试解决该问题。 另请注意,上面的代码可能会导致无限循环 - 因为 updateAttribute 调用本身会触发 'after save' 挂钩 - 这是我不太确定的另一个原因你真的问了。
我想更新 "after save" 挂钩中的最后一个 inserted/updated 文档(行),而不创建像这样的新实例:
Model.observe('after save', function (ctx, next) {
ctx.someProperty = 'Foo';
ctx.update();
});
怎么可能?
我不确定你所说的 'update' 模型是什么意思。据我所知,通用模型 class 上没有 update()
功能。如果您正在寻找 updateAttribute
,那么有关该功能的文档是 here.
但是,假设您的问题只是 "How do I access the observed model inside of a loopback hook?",那么答案是实例存储在 ctx.instance
而不是作为 ctx
变量本身返回。请参阅示例 here。
例如
Model.observe('after save', function (ctx, next) {
ctx.instance.updateAttributes({someProperty: 'Foo'})
});
如果您可以更详细地描述您尝试使用 update()
函数实现的功能,我将尝试解决该问题。 另请注意,上面的代码可能会导致无限循环 - 因为 updateAttribute 调用本身会触发 'after save' 挂钩 - 这是我不太确定的另一个原因你真的问了。