简体   繁体   English

NestJs 无法解析 HeicService 的依赖关系

[英]NestJs can't resolve dependencies of the HeicService

I am getting the following error when trying to spin up nestjs尝试启动 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 */ ]
  })

But as from my understanding I am doing everything right regards import/export of Modules, no circular dependency and so on.但据我所知,关于模块的导入/导出、没有循环依赖等,我所做的一切都是正确的。 Here are my modules:这是我的模块:

App应用程序

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

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

Config配置

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

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

ConfigService配置服务

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

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

Redis 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 Service Redis 发布/订阅服务

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 Cache Service 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);
      }
    }

Heic海克

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 {}

Service服务

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,
      ) {}

Anyone an idea what I am doing wrong?任何人都知道我做错了什么?

You're using @Inject() in your constructors with no injection token.您在没有注入令牌的构造函数中使用@Inject() You should be passing the injection token you are wanting to inject here.应该在此处传递要注入的注入令牌。 The HeicService 's constructor would then look something like this: HeicServiceconstructor看起来像这样:

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

The other option, as you're already using classes for the RedisCacheService and RedisPublishService is to just remove the @Inject() decorators all togehter for the HeicService另一种选择,因为您已经在使用RedisCacheServiceRedisPublishService的类,所以只需删除HeicService@Inject()装饰器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM