How to write remote hook for a method with parameter and relation

别说谁变了你拦得住时间么 提交于 2019-12-10 23:09:08

问题


I went through the remote hook documentation, I can successfully create remote hooks for methods without extra parameters, like login, which is:

customer.afterRemote('login', function(ctx, modelInstance, next) {
      if (ctx.result) {
          ...
          next();
      }
      else{
          next();
      }
    });

Now, How to write a remote hook for a method say :

GET /customers/{id}

POST /customers/{id}

or while posting related objects like

POST /customers/{id}/contacts
GET /customers/{id}/contacts

I know doing the following with POST /customers/{id}/contacts:

customer.beforeRemote('**', function(ctx, user, next) {
  console.log(ctx.methodString, 'was invoked remotely'); // customers.prototype.save was invoked remotely
  next();
});

would return the name of the method called like:

customer.prototype.__create__contacts was invoked remotely

But I am still unable to hook it specifically, and following tries are to no avail, and hook isn't being reached:

customer.beforeRemote('customer.prototype.__create__contacts', function(ctx, user, next)

customer.beforeRemote(customer.prototype.__create__contacts, function(ctx, user, next)

回答1:


Found out! The answer lies here

First catch the method name using customer.beforeRemote('**', function(ctx, user, next) as I mentioned in question, then simply following would work:

customer.beforeRemote('*.__create__assets', function(ctx, user, next) {
      console.log(ctx.methodString, 'was invoked remotely with customers'); // customers.prototype.save was invoked remotely
      next();
    });


来源:https://stackoverflow.com/questions/37696417/how-to-write-remote-hook-for-a-method-with-parameter-and-relation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!