Search code examples
loopbackjsstrongloop

RemoteHooks with wildcards doen't work in storage container


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 ?


Solution

  • 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