【问题标题】:I can't Inject class by identifier for interfaces我无法通过接口的标识符注入类
【发布时间】:2020-07-10 16:00:15
【问题描述】:

我猜是因为我是用翻译功能写句子的,所以很难读,对不起。

我正在尝试基于接口注入提供程序。 下面是我的代码。

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

Blob 类实现了IStorageBase 接口。 而CoreModule 使基于IStoraegBase 的提供程序可用于其他模块。

示例:

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

但是,执行此操作时,会输出以下错误。

[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

我认为该接口不能注入到模块装饰器的“提供者”或“导入”中。 如何解决上述错误并正常运行程序?

谢谢。

【问题讨论】:

    标签: nestjs


    【解决方案1】:

    Nest 存在问题,因为在您的 BlobService 中,您依赖于 BlobDependencies,但在您的 CoreModule 中的任何地方都没有提供这些依赖项。除此之外,由于它是自定义类型而不是类,它会在运行时转换为object,这就是为什么 Nest 会给出Nest can't resolve dependencies of the IStorageBase (?). Please make sure that the argument Object at index [0] is available in the CoreModule context. 的错误。这里要注意的重要一点是它在CoreModule 的上下文中谈论IStorageBase,这意味着CoreModule 是错误所在。要解决此问题,请为 Nest 注入某种依赖项,并确保添加 @Inject() 装饰器,因为 Nest 将无法为 Object 进行一般注入

    【讨论】:

    • 我忽略了自定义类型依赖!通过将'core.module.ts'中的自定义类型注册为自定义提供者来解决!非常感谢。
    猜你喜欢
    • 2019-04-17
    • 1970-01-01
    • 2016-09-12
    • 2021-04-11
    • 2017-11-21
    • 1970-01-01
    • 2021-05-04
    • 2015-04-06
    • 1970-01-01
    相关资源
    最近更新 更多