Loopback beforeRemote for PUT requests
Asked Answered
S

5

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");
});
Spectrohelioscope answered 17/12, 2015 at 4:36 Comment(2)
Have you tried "*.save" as the method name?Bullivant
*. save is an operation hook i am interested in remote hookSpectrohelioscope
S
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();
});
Swifter answered 7/9, 2018 at 13:48 Comment(0)
E
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();
});
Erythropoiesis answered 22/12, 2015 at 18:25 Comment(2)
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]Spectrohelioscope
@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.Erythropoiesis
H
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.

Horsehair answered 3/8, 2016 at 7:6 Comment(0)
G
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".

Gardie answered 20/7, 2018 at 9:17 Comment(1)
Thanks!, The docs really bumped me up. MyUser.disableRemoteMethodByName("prototype.updateAttributes"); // disables PATCH /MyUsers/{id}Unscrupulous
C
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();
});
Compact answered 8/9, 2021 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.