【问题标题】:Error: Nest can't resolve dependencies of the AwsSnsService (?)错误:Nest 无法解析 AwsSnsService (?) 的依赖项
【发布时间】:2021-10-17 22:27:11
【问题描述】:

我正在尝试将服务从一个模块注入另一个模块。我一直在努力

Error: Nest can't resolve dependencies of the AwsSnsService (?). Please make sure that the argument function() {\n if (klass !== Object) {\n return klass.apply(this, arguments);\n }\n } at index [0] is available in the AwsModule context.

下面是代码

aws-sns.service.ts

@Injectable()
export class AwsSnsService {
  constructor(private readonly awsSNS: AWS.SNS) {}
}

aws.module.ts

import { Module } from '@nestjs/common';
import { AwsSnsService } from './aws-sns.service';

@Module({
  providers: [AwsSnsService],
  exports: [AwsSnsService],
})
export class AwsModule {}

我想在我的用户模块中使用 AwsSnsService。我正在按照以下方式进行操作

@Module({
  imports: [
    SomeOtherModule,
    AwsModule,
  ],
  providers: [UserService, UserDevicePushTokensService],
  exports: [UserService, UserDevicePushTokensService],
})
export class UserModule {}

@Injectable()
export class UserDevicePushTokensService {
  constructor(private readonly awsSnsService: AwsSnsService) {}
}

在我看来,这些点以正确的方式连接起来。还是没搞清楚。

【问题讨论】:

  • AWS.SNS 是 Nest 提供商吗?这就是它似乎失败的地方。
  • @BunyamiN 不,它不是巢提供者。也许你是对的,这可能是一个问题

标签: javascript node.js typescript dependency-injection nestjs


【解决方案1】:

您必须在 AwsSnsService 中解决 AWS.SNS 的依赖关系

以我的 s3 为例,SNS 和任何服务都一样:

s3.provider.ts

export const s3Providers: Provider = {
  provide: S3_PROVIDER_KEY,
  useFactory: async (): Promise<S3> => {
    return new S3({
      accessKeyId: "Key",
      secretAccessKey: "AccessKey",
    });
  },
};

s3.service.ts

@Injectable()
export class S3Service {
  constructor(
     @Inject(S3_PROVIDER_KEY)
     private readonly s3: S3) {}
}

你可以在这里阅读更多

Nestjs custom provider

【讨论】:

    猜你喜欢
    • 2021-06-04
    • 2023-02-04
    • 2022-11-12
    • 2020-08-12
    • 2023-04-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多