Answer the question
In order to leave comments, you need to log in
How to set a decorator on a method that is not an endpoint?
Hello!
Question on NestJS.
The task is to make sure that when the controller's secondMethod method is called from the firstMethod method, a custom decorator is triggered. At the moment this is not happening. I noticed that only those decorators that are hung on endpoints work, that is, on methods that are marked with Get, Post, Patch, etc. decorators.
class Controller {
@Get('/')
firstMethod() {
secondMethod()
}
@CustomDecorator()
secondMethod() {}
}
Answer the question
In order to leave comments, you need to log in
Here's how to make a decorator - it will work:
const CustomDecorator = () => (target: Object, propertyKey: string, descriptor: PropertyDescriptor) => {
let sourceMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
console.log("before");
let result = sourceMethod.apply(this, args);
console.log("after");
return result;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question