Node.js 类型为'的参数;globalThis'的类型;不可分配给类型为';入口服务';

Node.js 类型为'的参数;globalThis'的类型;不可分配给类型为';入口服务';,node.js,nestjs,Node.js,Nestjs,我试图将我的服务传递给我传递给方法装饰器的类的实例 服务内容如下: @Injectable() export class EntryService { constructor( @InjectRepository(EntryEntity) private readonly entryRepository: Repository<EntryEntity>, @InjectRepository(ImageEntity)

我试图将我的服务传递给我传递给方法装饰器的类的实例

服务内容如下:

@Injectable()
export class EntryService {
    constructor(
        @InjectRepository(EntryEntity)
        private readonly entryRepository: Repository<EntryEntity>,
        @InjectRepository(ImageEntity)
        private readonly imageRepository: Repository<ImageEntity>,
        private readonly awsService: AwsService,
        private readonly connection: Connection,
        private readonly categoriesService: CategoriesService,
        private readonly cacheService: CacheService,
        private readonly usersService: UserService,
        private readonly imagesService: ImagesService,
        private readonly notificationService: NotificationsService,
    ) {}

    @RecordEntryOperation(new CreateOperation(this))
    public async create(createEntryDto: CreateEntryBodyDto): Promise<Entry> {
      const queryRunner = this.connection.createQueryRunner();
      await queryRunner.connect();
      await queryRunner.startTransaction();
      try {
        await queryRunner.commitTransaction();
        // more code
      } catch (err) {
        await queryRunner.rollbackTransaction();
      } finally {
        await queryRunner.release();
      }
    }
}
CreateOperation
类如下所示(尚未完全实现):

我不完全明白这个错误是怎么回事。我怀疑这意味着传递给
CreateOperation
类的
this
没有通过依赖项注入器将所有这些依赖项注入到服务中

我尝试了不同的方法,但没有效果。好像我不完全明白发生了什么

有什么想法吗


那么,构建代码的正确方法是什么?

问题在于以下几行:

@RecordEntryOperation(new CreateOperation(this))
并不像您预期的那样引用了
EntryService
的实例,而是引用了
全局此
(即
实际上引用了当前模块),因此出现了错误。您可以做的是稍微更改
操作
-class,并将
入口服务
传递给execute方法

export class CreateOperation extends Operation {
    constructor() { super(); }

    public async execute(entryService: EntryService): Promise<any> {
        return entryService.someEntryServiceOperation();
    }
}
然后将其用于:

@RecordEntryOperation(CreateOperation)
public async create(createEntryDto: CreateEntryBodyDto): Promise<Entry> { .. }
@RecordEntryOperation(CreateOperation)
公共异步创建(createEntryDto:CreateEntryBodyDto):承诺{..}

有一个小错误,请重新加载并使用最新版本:)尝试运行此代码,看起来一切都很顺利,但是
这个。由于某种原因,
RecordEntryOperation
中的entryService
未定义的
。哦,它实际上应该是
这个
因为
这个
就是
entryService
,正确的?看我的编辑。啊,是的,对了,我没有注意到,现在它似乎像预期的那样工作!谢谢
@RecordEntryOperation(new CreateOperation(this))
export class CreateOperation extends Operation {
    constructor() { super(); }

    public async execute(entryService: EntryService): Promise<any> {
        return entryService.someEntryServiceOperation();
    }
}
export const RecordEntryOperation = (OperationType: typeof CreateOperation) => {
    return (target: object, key: string | symbol, descriptor: PropertyDescriptor) => {      
        const operation = new OperationType(); 
        const original = descriptor.value;
        descriptor.value = async function(...args: any[]) {
            const response = await original.apply(this, args);
            console.log(`operation.execute()`, await operation.execute(this));
            return response;
        };
    };
};
@RecordEntryOperation(CreateOperation)
public async create(createEntryDto: CreateEntryBodyDto): Promise<Entry> { .. }