NestJs 无法解析 HeicService 的依赖关系

NestJs can't resolve dependencies of the HeicService

尝试启动 nestjs 时出现以下错误

    [Nest] 47548  - 04/23/2022, 10:41:12 AM   ERROR [ExceptionHandler] Nest can't resolve dependencies of the HeicService (?, +). Please make sure that the argument dependency at index [0] is available in the HeicModule context.

Potential solutions:
- If dependency is a provider, is it part of the current HeicModule?
- If dependency is exported from a separate @Module, is that module imported within HeicModule?
  @Module({
    imports: [ /* the Module containing dependency */ ]
  })

但据我所知,我在模块 import/export 方面所做的一切都是正确的,没有循环依赖等等。这是我的模块:

应用程序

import { Module } from '@nestjs/common';
import { EurekaModule } from './eureka/eureka.module';
import { HeicModule } from './heic/heic.module';

    @Module({
      imports: [HeicModule, EurekaModule],
    })
    export class AppModule {}

配置

import { Module } from '@nestjs/common';
import { ConfigService } from './config.service';

    @Module({
      providers: [ConfigService],
      exports: [ConfigService],
    })
    export class ConfigModule {}

配置服务

import { Injectable } from '@nestjs/common';
import { Config } from './config.interface';

@Injectable()
export class ConfigService {
  private readonly map: Config;

Redis

import { CacheModule, Module } from '@nestjs/common';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { ConfigModule } from '../config/config.module';
import { ConfigService } from '../config/config.service';
import { RedisCacheService } from './redis-cache.service';
import * as redisStore from 'cache-manager-redis-store';
import { RedisPublishService } from './redis-publish.service';
    
@Module({
          imports: [
            CacheModule.register({
              imports: [ConfigModule],
              inject: [ConfigService],
              useFactory: async (configService: ConfigService) => ({
                store: redisStore,
                host: configService.get('host'),
                port: configService.get('port'),
                keyPrefix: configService.get('keyPrefix'),
                userName: configService.get('username'),
                password: configService.get('password'),
                ttl: configService.get('cacheTTL'),
              }),
            }),
            ClientsModule.register([
              {
                name: 'PUBLISH_SERVICE',
                transport: Transport.REDIS,
                options: {
                  url: 'redis://localhost:6379',
                },
              },
            ]),
          ],
          providers: [RedisCacheService, RedisPublishService],
          exports: [RedisCacheService, RedisPublishService],
        })
        export class RedisModule {}

Redis Pub/Sub 服务

import { Inject, Injectable } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { ImageMessage } from './ImageMessage'; 
      
    @Injectable()
        export class RedisPublishService {
          private readonly CHANNEL: string = 'heic-image-result';
          constructor(@Inject('PUBLISH_SERVICE') private client: ClientProxy) {}
        
          async publishMessage(imageMessage: ImageMessage) {
            this.client.emit({ cmd: this.CHANNEL }, imageMessage);
          }
        }

Redis 缓存服务

import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
import { Cache } from 'cache-manager';
import { ImageMessage } from './ImageMessage';

    @Injectable()
    export class RedisCacheService {
      constructor(@Inject(CACHE_MANAGER) private readonly cache: Cache) {}
    
      async get(key): Promise<ImageMessage> {
        return this.cache.get(key);
      }
    
      async set(key, value) {
        await this.cache.set(key, value, 120);
      }
    }

海克

import { Module } from '@nestjs/common';
import { RedisModule } from '../redis/redis.module';
import { HeicService } from './heic.service';
import { MessageListenerController } from './message-listener.controller';

    @Module({
      imports: [RedisModule],
      controllers: [MessageListenerController],
      providers: [HeicService],
      exports: [HeicService],
    })
    export class HeicModule {}

服务

import { Inject, Injectable } from '@nestjs/common';
import { ImageMessage } from '../redis/ImageMessage';
import { RedisCacheService } from '../redis/redis-cache.service';
import { RedisPublishService } from '../redis/redis-publish.service';
import { OutputFormatEnum } from './output-format.enum';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const convert = require('heic-convert');

    @Injectable()
    export class HeicService {
      constructor(
        @Inject() private readonly redisPublishService: RedisPublishService,
        @Inject() private readonly redisCache: RedisCacheService,
      ) {}

有人知道我做错了什么吗?

您在没有注入令牌的构造函数中使用 @Inject()。您应该 在此处传递您要注入的注入令牌。 HeicServiceconstructor 看起来像这样:

@Injectable()
export class HeicService {
  constructor(
    @Inject(RedisPublishService) private readonly redisPublishService: RedisPublishService,
    @Inject(RedisCacheService) private readonly redisCache: RedisCacheService,
  ) {}
}

另一种选择,因为您已经在 RedisCacheServiceRedisPublishService 中使用 类,所以只需删除 @Inject() 装饰器即可 HeicService