NestCrafts / nestjs-minio

Your favorite object storage with nestjs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Register Async Unresolved Dependency

blitzcaster opened this issue · comments

I'm having trouble using registerAsync method for dynamic configurations.

@Module({
  imports: [
    ConfigModule,
    NestMinioModule.registerAsync({
      imports:[ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => ({
        endPoint: configService.get<string>('S3_ENDPOINT'),
        port: configService.get<number>('S3_PORT'),
        useSSL: configService.get<boolean>('S3_USE_SSL'),
        region: configService.get<string>('S3_REGION'),
        accessKey: configService.get<string>('S3_ACCESS'),
        secretKey: configService.get<string>('S3_SECRET')
      } as NestMinioOptions)
    })
  ],
  controllers: [FileController],
  providers: [FileService],
})
export class FileModule {}

This way I kept getting unresolved dependency error. Even when config module is already imported.

Nest can't resolve dependencies of the NEST_MINIO_OPTIONS (?). Please make sure that the argument ConfigService at index [0] is available in the NestMinioModule context.

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

I've looked for solutions but still can't find one. Also other modules works fine with registerAsync. Any help is appreciated, thanks in advance.

can you share a repo so i can reproduce the issue @blitzcaster

@Module({
  imports: [
    NestMinioModule.registerAsync({
      useFactory :async() => ({
        endPoint: 'play.min.io',
        port: 9000,
        useSSL: true,
        accessKey: 'Q3AM3UQ867SPQQA43P2F',
        secretKey: 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG',
      })
    })],
  controllers: [AppController],
  providers: [AppService],
})

This works as intended. Closing the issue

Hi, I've the same issue.

It looks like registerAsync is not able to inject the correct dependencies to the factory function.

@blitzcaster did you find a way to fix the issue?

@massimeddu I haven't solve it, I use a workaround for the time being. This is done by using process.env directly. Messy workaround I know, but it works. The only annoying problem that this workaround is not working when using .env file.

@rubiin sorry I'm only replying this now. My project have moved on and I kind of forgot about this. I also can't share the repo, it belongs to my employer not me. Maybe @massimeddu can help in that regard.

@rubiin here the minimum code snippet to reproduce the issue. With the inject line commented it works, if you uncomment the line you should see the issue.

Thanks.

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { NestMinioModule } from 'nestjs-minio';

@Module({
  imports: [
    ConfigModule,
    NestMinioModule.registerAsync({
      imports: [ConfigModule],
      // FIXME: If you uncomment the following line you we'll get the error
      // inject: [ConfigService],
      useFactory: async () =>
         ({
          endPoint: 'minio',
          port: 9000,
          useSSL: false,
          accessKey: 'minio',
          secretKey: 'minio123',
        })
      ,
    }),
  ],
})
export class MediaModule {
}

let me look on this then. I tried the snippet above so i could not actually pin point the problem. Let me try to reproduce the issue or anyone has a repo ?

Hi @rubiin , sorry i cant share my private repo.

Anyway, I found the issue and pushed a PR to fix it.

Thanks

@massimeddu Thanks for the contributon. I have merged it

The fix is available in new version 2.0.2

@rubiin Exactly same issue appears in v2.2.3

@rubiin Exactly same issue appears in v2.2.3

will look into it

@rubiin Isn't because i'm using nestjs v9 ?

@Romanchuk can you try the preminor release 2.3.0-0

@rubiin 2.3.0-0
Both register and registerAsync have a same exception (different than previous)

 Nest can't resolve dependencies of the NestMinioService (?). Please make sure that the argument CONFIGURABLE_MODULE_OPTIONS[b787fbe3-98f5-48b6-bad1-43ba673f20c5] at index [0] is available in the NestMinioModule context.

Potential solutions:
- If CONFIGURABLE_MODULE_OPTIONS[b787fbe3-98f5-48b6-bad1-43ba673f20c5] is a provider, is it part of the current NestMinioModule?
- If CONFIGURABLE_MODULE_OPTIONS[b787fbe3-98f5-48b6-bad1-43ba673f20c5] is exported from a separate @Module, is 
that module imported within NestMinioModule?
  @Module({
    imports: [ /* the Module containing CONFIGURABLE_MODULE_OPTIONS[b787fbe3-98f5-48b6-bad1-43ba673f20c5] */ ] 
  })

The way i use:

NestMinioModule.registerAsync({
	isGlobal: true,
	useFactory: () => ({
		endPoint: 'plotform-minio',
		port: 9000,
		useSSL: false,
		accessKey: 'admin',
		secretKey: 'password'
	})
})

@rubiin One of my modules broke whole app... It had NestMinioModule import. I did flat module structure and now it starts.
Still have to check how it will work with well orginized module structure, but i think this particular issue is solved.
Thank you very much!

@rubiin Oh, i get this error on bootstrap when:
constructor(@Inject(MINIO_CONNECTION) private readonly client: Client) {}
This works fine:
constructor(@Inject(NestMinioService) private readonly client: NestMinioService) {}
Will test tomorrow how requests works

closing this as fixed on https://github.com/rubiin/nestjs-minio/releases/tag/2.3.0 . Feel free to reopen if issue persists