Decorators in commands #4190
-
Hi all, I want to use decorated "run" method in command. My command is defined like so: import { BaseCommand } from '@adonisjs/core/build/standalone';
import ConversionService from 'App/Services/ConversionService';
import { loggedCommand } from 'Commands/index';
export default class ConvertPaymentsAmount extends BaseCommand {
public static commandName = 'convert:payments_amount';
public static description = 'not important';
public static settings = {
loadApp: true,
};
@loggedCommand()
public async run(): Promise<{ paymentsConverted: number } | undefined> {
return ConversionService.convertMissingPaymentsAmount(20, 100);
}
} The decorator is a pretty standard one (used for logging command output and performance to database): export const loggedCommand = (): MethodDecorator => {
return function (target: any, _propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = async function (...args: Array<any>) {
// DB logging code
};
return descriptor;
};
}; All goes well until I want to run I am getting TypeError: (0 , index_1.loggedCommand) is not a function and the import is undefined when I am trying to log it. What might be the cause and how is the workaround? Am I doing anything wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @lukfor85! 👋🏻 Since you are using a custom path, ensure you have registered it inside the Also, to ensure this is not an issue about loading, you can try to load your decorator with a relative path. |
Beta Was this translation helpful? Give feedback.
Hey @lukfor85! 👋🏻
Since you are using a custom path, ensure you have registered it inside the
.adonisrc.json
file and thetsconfig.json
file.Also, to ensure this is not an issue about loading, you can try to load your decorator with a relative path.