Home > Net >  NestJs can't resolve dependencies of the HeicService
NestJs can't resolve dependencies of the HeicService

Time:04-24

I am getting the following error when trying to spin up 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

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

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

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?

CodePudding user response:

You're using @Inject() in your constructors with no injection token. You should be passing the injection token you are wanting to inject here. The HeicService's constructor would then look something like this:

@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

  • Related