I’m trying to use a service inside a decorator and this decorator is used over ServiceX function, which is called from some serviceY. However getting an error. Spent quite a long time with no success. Any help will be appreciated.
Error: Nest can't resolve dependencies of ServiceX. Please make sure that the "logger" property is available in the current context.
// myDecortor.ts
import { Inject } from '@nestjs/common';
import { LoggerService } from '../../logger/logger.service';
export function myDecorator(bubble = true) {
const injectLogger = Inject(LoggerService);
return (target: any, propertyKey: string, propertyDescriptor: PropertyDescriptor) => {
injectLogger(target, 'logger'); // This logger property is not accessible to ServiceX
//get original method
const originalMethod = propertyDescriptor.value;
//redefine descriptor value within own function block
propertyDescriptor.value = async function(...args: any[]) {
// something
};
};
}
importing LoggerModule in ServiceXModule
// serviceX.module.ts
import { LoggerModule } from '../../logger/logger.module';
/// .....
@Module({
imports: [LoggerModule],
providers: [serviceX],
exports: [ServiceX],
})
Using decortor over a method of ServiceX class.
// serviceX.service.ts
import { myDecorator } from '../logger/myDecorator';
@myDecorator()
async serviceXfunction() {
//some more stuff
}
Importing serviceX module in ServiceY
// serviceY.module.ts
import { ServiceXModule } from '../../logger/serviceX.module';
/// .....
@Module({
imports: [ServiceXModule],
providers: [serviceY],
exports: [ServiceY],
})
// serviceY.service.ts
import { ServiceX } from '../logger/serviceX.service';
constructor(private readonly serviceX: ServiceX){}
async serviceYfunction() {
await serviceX.serviceXfunction();
}