0
votes

I guess it's hard to read because I use the translation function to write sentences, sorry.

I am trying to inject a provider based on an interface. Below is my code.

storage-base.interface.ts

export interface IStorageBase {
  upload(fileName: string): Promise<string>;
  deleteLocalFile(fileName: string): Promise<void>;
}

blob.service.ts

import { Injectable } from '@nestjs/common';
import { BlobServiceClient, ContainerClient } from '@azure/storage-blob';
import { IStorageBase } from './storage-base.interface';

type BlobDependencies = {
  connectionString: string;
  containerName: string;
};

@Injectable()
export class BlobService implements IStorageBase {
  private readonly blobServiceClient: BlobServiceClient;
  private readonly containerClient: ContainerClient;

  constructor(blobDependencies: BlobDependencies) {
    //Some kind of processing
  }

  upload(filename: string): Promise<string> {
    //Some kind of processing
  }

  deleteLocalFile(filename: string): Promise<void> {
    //Some kind of processing
  }

  private getAbsolutePath(filename: string): string {
    //Some kind of processing
  }
}

core.module.ts

import { Module } from '@nestjs/common';
import { BlobService } from './blob.service';

const blobServiceProvider = { provide: 'IStorageBase', useClass: BlobService };

@Module({
  providers: [blobServiceProvider],
  exports: [blobServiceProvider],
})
export class CoreModule {}

The Blob class implements the IStorageBase interface. And CoreModule makes providers based on IStoraegBase available to other modules.

example:

video-cut-out.module.ts

import { Module } from '@nestjs/common';
import { VideoCutOutService } from './video-cut-out.service';
import { VideoCutOutController } from './video-cut-out.controller';
import { CoreModule } from '../core/core.module';

@Module({
  imports: [CoreModule],
  providers: [VideoCutOutService],
  controllers: [VideoCutOutController],
  exports: [],
})
export class VideoCutOutModule {}

video-cut-out.service.ts

import { Injectable, Inject } from '@nestjs/common';
import { VideoCutOutRequest, VideoCutOutResponse } from './video-cut-out.model';
import { IStorageBase } from '../core/storage-base.interface';

@Injectable()
export class VideoCutOutService {
  constructor(@Inject('IStorageBase') private _blob: IStorageBase) {}
  async executeAsync(
    videoCutOutRequest: VideoCutOutRequest,
  ): Promise<VideoCutOutResponse> {
    //Some kind of processing
  }
}

However, when this is executed, the following error is output.

[Nest] 18352   - 2020-03-30 17:04:12   [ExceptionHandler] Nest can't resolve dependencies of the IStorageBase (?). Please make sure that the argument Object at index [0] is available in the CoreModule context.

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

I think that interface cannot be injected into "providers" or "imports" of Module Decorator. How can I resolve the above error and run the program normally?

thank you.

1

1 Answers

0
votes

Nest is having an issue because in your BlobService you have dependencies on BlobDependencies, but you don't have those being provided anywhere in your CoreModule. Along with that, as it is a custom type and not a class, it gets converted to an object at runtime, which is why Nest gives you the error of Nest can't resolve dependencies of the IStorageBase (?). Please make sure that the argument Object at index [0] is available in the CoreModule context.. The important thing to notice here is that it is talking about IStorageBase in the context of CoreModule, meaning that CoreModule is where the error is. To fix this, provide some sort of dependency for Nest to inject and make sure to add the @Inject() decorator, as Nest will not be able to inject generically for Object