1

I am using Loopback storage service for file uploads, it works fine. I want to do post-processing for uploaded files, so I added remote hooks methods beforeRemote and afterRemote. When i use '*' for method name it works. When I change it to 'upload' or 'container.upload' or anything else it stops working. Even afterSave is not called for container.

module.exports = function(Container) {

 Container.beforeRemote('*.upload', function(ctx, unused, next) {
  if(ctx.req.accessToken) {
     console.log('beforeCalled with token');
   next();
  } else {
    console.log('beforeCalled no token');
    next();
  }
 });

 Container.afterRemote('*.upload', function(ctx, user, next) {
   console.log("file uploaded", user.result.files);
   next();
  });

 Container.beforeSave = function(next, modelInstance) {
  console.log("beforeSave:", modelInstance );
  next();
 };

};

Am I missing something ? How it should be done ?

Kulver B.
  • 13
  • 2

2 Answers2

1

You can work around it with this for now:

Container.beforeRemote('**', function(ctx, unused, next) {
  if(ctx.methodString === ‘upload’) {
    ...
  }
}

See https://groups.google.com/forum/#!topic/loopbackjs/EI23RYX9C9M

superkhau
  • 2,781
  • 18
  • 9
0

For this particular case either Container.beforeRemote('**.upload', function(ctx, unused, next) or Container.beforeRemote('upload', function(ctx, unused, next) should be working. Would be good to get feedback from loopback developers what is the proper way though.

Alex V
  • 1,155
  • 8
  • 10
  • Unfortunately it doesn't work, Only works with `"*"` – Kulver B. Dec 03 '14 at 14:27
  • Which version of loopback do you have? I just tested both and they work for me. – Alex V Dec 03 '14 at 19:03
  • strongloop v2.9.2 (node v0.10.33), strange that it works for you....because i was debugging it, and I found that in afterRemote handling , remotes.container._methods are empty.... – Kulver B. Dec 08 '14 at 10:06