问题
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