Reputation: 791
I want to update last inserted/updated document(row) in "after save" hook without create new instance of that like this:
Model.observe('after save', function (ctx, next) {
ctx.someProperty = 'Foo';
ctx.update();
});
How it possible?
Upvotes: 4
Views: 4699
Reputation: 2385
I'm not sure what you mean by 'update' a model. As far as I know, there is no update()
function on the generic model class. If you're looking for updateAttribute
then the documentation on that functionality is here.
However, assuming your question is just "How do I access the observed model inside of a loopback hook?" then the answer is that the instance is stored at ctx.instance
rather that returned as ctx
variable itself. See the examples here.
E.g.
Model.observe('after save', function (ctx, next) {
ctx.instance.updateAttributes({someProperty: 'Foo'})
});
If you can describe in more detail the functionality you're trying to achieve with the update()
function, I will try to address that question. Note also that the code above would probably cause an infinite loop - because the updateAttribute call will itself trigger the 'after save' hook - which is another reason why I'm not so sure what you're really asking.
Upvotes: 5