【问题标题】:RemoteHooks with wildcards doen't work in storage container带有通配符的远程挂钩在存储容器中不起作用
【发布时间】:2014-12-06 03:55:35
【问题描述】:

我正在使用Loopback storage service 进行文件上传,它工作正常。 我想对上传的文件进行后期处理,所以我添加了远程挂钩方法 beforeRemoteafterRemote。当我使用 '*' 作为方法名称时,它可以工作。当我将其更改为 'upload''container.upload' 或其他任何内容时,它会停止工作。 甚至 afterSave 也不会被容器调用。

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();
 };

};

我错过了什么吗?应该怎么做?

【问题讨论】:

    标签: loopbackjs strongloop


    【解决方案1】:

    你现在可以解决这个问题:

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

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

    【讨论】:

    • Update#1 Container.afterRemote('upload',... 现在可以使用了
    • Update#2 对于Container.afterRemote('**', ...,条件比较字符串现在是container.upload,而不是upload
    【解决方案2】:

    对于这种特殊情况,Container.beforeRemote('**.upload', function(ctx, unused, next)Container.beforeRemote('upload', function(ctx, unused, next) 应该可以工作。最好从环回开发人员那里获得反馈,但正确的方法是什么。

    【讨论】:

    • 不幸的是它不起作用,只适用于"*"
    • 你有哪个版本的环回?我刚刚测试了两者,它们对我有用。
    • strongloop v2.9.2 (node v0.10.33),奇怪的是它对你有用....因为我正在调试它,我发现在 afterRemote handling , remotes.container._methods 是空的....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2014-11-30
    • 2023-03-06
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多