In Nest.js, how to get a service instance inside a decorator?

Answer

Late to the party, but since I had a similar problem (Use global nest module in decorator) and stumbled upon this question.

import { Inject } from '@nestjs/common';
export function yourDecorator() {
const injectYourService = Inject(YourServiceClass);

return (target: any, propertyKey: string, propertyDescriptor: PropertyDescriptor) => {
// this is equivalent to have a constructor like constructor(yourservice: YourServiceClass)
// note that this will injected to the instance, while your decorator runs for the class constructor
injectYourService(target, 'yourservice');

// do something in you decorator

// we use a ref here so we can type it
const yourservice: YourServiceClass = this.yourservice;
yourservice.someMethod(someParam);
};
}

All react js Questions

Ask your interview questions on react-js

Write Your comment or Questions if you want the answers on react-js from react-js Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---