Search code examples
node.jsnestjsminio

Nestjs this.minioClientService.upload is not a function


This is my minio-client.service:

@Injectable()
export class MinioClientService {
  private readonly logger = new Logger(MinioClientService.name);
  private readonly defaultBucketName = 'default';

  constructor(private minio: Minio.Client) {}

  async upload(
    file: Express.Multer.File,
    bucketName: string = this.defaultBucketName,
  ) {
   // some code & logic
  }
}

I imported the above service in my admins.service:

@Injectable()
export class AdminsService {
  constructor(
    @InjectRepository(AdminsRepository)
    // this line 👇
    private minioClientService: MinioClientService,
    private adminsRepository: AdminsRepository,
  ) {}

  async update(file) {
    if (file) {
      // error happens here 👇
      const uploadedImage = await this.minioClientService.upload(file);
      console.log(uploadedImage);
    }
  }

And Error Message: enter image description here

I also imported minio-client.module in admins.moudle and there is no error when starting application. The error happens when this update method is called.


Solution

  • You have your @InjectRepository() decorator in the wrong spot. That decorator is now telling Nest to inject AdminsRepository for the parameter that is supposed to be MinioClientService, but this is outside of what Typescript can read. Use this for your constructor instead

      constructor(
        // this line 👇
        private minioClientService: MinioClientService,
        @InjectRepository(AdminsRepository)
        private adminsRepository: AdminsRepository,
      ) {}
    

    And it should work just fine.