5

Using Loopback framework, I want to perform some operations before the Item is edited hence I am trying this but unable to bind this to the update hook.

  Item.beforeRemote("update", function(ctx,myitem,next) {
   console.log("inside update");
  });

Instead of update I have tried with updateAttributes,updateById, create but none works. This kind of beforeRemote hook works well with create on POST, but unable to get it with PUT during edit. The last solution left with me is again inspect the methodString with wildcard hook but I want to know if there is anything documented which I could not find.

Item.beforeRemote("**",function(ctx,instance,next){
  console.log("inside update");
});
Swapnil Gondkar
  • 345
  • 1
  • 4
  • 12

5 Answers5

7

I know that two year have passed since this post was opened, but if any body have the same question and if you use the endpoint your_model/{id} the afterRemote hook is replaceById. If you need to know which method is fired in remote hook use this code:

yourModel.beforeRemote('**', function(ctx, unused, next) {
    console.info('Method name: ', ctx.method.name);
    next();
});
5

Contrary to the comments, save is a remote hook, not an operation hook, but you want to use it as: prototype.save. The relevant operational hook would be before save. You can see a table of these on the LoopBack docs page. I would probably implement this as an operational hook though, and use the isNewInstance property on the context to only perform the action on update:

Item.observe('before save', function(ctx, next) {
  if (ctx.isNewInstance) {
    // do something with ctx.currentInstance
  }
  next();
});
Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
  • 1
    I want to parse multipart using formidable, I use the loopback storage container's upload method for that through which I get the relevant fields. **These fields are unavailable in operation hooks**.Secondly save which you say is given in the documentation as remote hook is clearly mentioned that it is a custom remote hook means any method whose name ends with save, please read doc: **This remote hook is called whenever any remote method whose name ends with "save" is executed:[code]** – Swapnil Gondkar Dec 29 '15 at 17:49
  • 2
    @SwapnilGondkar I appreciate your efforts to help clarify, but I work for StrongLooop, and I'm telling you that `save` is not an operation hook. There is `before save` and `after save`, but **not** `save` by itself. That is a "remote hook" on the prototype. Trust me, I helped write the docs. As for the fields not being available in the hook callback, that's not surprising. You might need to use a piece of middleware to add the fields to the request object so that they are available in the hook callback via the context. – Jordan Kasper Dec 29 '15 at 23:20
3

Sorry for bumping into old question but its for those who are still searching.

'prototype.updateAttributes' can be used as remote hook for update requests. and @jakerella , there is no remote hook called 'save' , i myself tried it, but didnt work.

Anil Jangra
  • 103
  • 2
  • 11
3

Came here looking for another thing, guess it will be helpful to someone. For before remote model/:id patch method you have to use "prototype.patchAttributes".

alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
cuddlemeister
  • 1,586
  • 12
  • 15
  • Thanks!, The [docs](https://loopback.io/doc/en/lb3/Authentication-authorization-and-permissions.html#exposing-and-hiding-models-methods-and-endpoints) really bumped me up. `MyUser.disableRemoteMethodByName("prototype.updateAttributes"); // disables PATCH /MyUsers/{id}` – hadifikri Nov 01 '18 at 10:37
0

On loopback3 for PATCH you can use "prototype.patchAttributes" to sanitize your data before the update.

YourModel.beforeRemote('prototype.patchAttributes', (ctx, unused, next) => { 
  console.log(ctx.args.data);
  next();
});