【问题标题】:Configuration setup : The "path" argument must be one of type[...] Received type undefined配置设置:“路径”参数必须是 type[...] 接收类型未定义之一
【发布时间】:2020-02-02 18:51:12
【问题描述】:

我跟着Configuration guide:

模块

@Module({
  providers: [
    {
      provide: ConfigService,
      useValue: new ConfigService(`development.env`),
    },
  ],
  exports: [ConfigService],
})
export class ConfigModule {}

服务:

export interface EnvConfig {
  [key: string]: string;
}
export class ConfigService {
  private readonly envConfig: EnvConfig;

  constructor(filePath: string) {
    console.log(filePath);
    const config = dotenv.parse(fs.readFileSync(filePath));
    this.envConfig = ConfigService.validateInput(config);
  }
[...]

每当我运行应用程序时:

> nest start


development.env
[Nest] 10496   - 10/04/2019, 2:16:49 PM   [NestFactory] Starting Nest application...
undefined
[Nest] 10496   - 10/04/2019, 2:16:49 PM   [ExceptionHandler] The "path" argument must be one of ty
pe string, Buffer, or URL. Received type undefined +14ms
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be one of type string, Buffer, or URL.
Received type undefined

该服务似乎被实例化了 2 次。我不知道为什么。知道这里发生了什么吗?

【问题讨论】:

  • 您的ConfigService 只在providers 数组中出现一次?没有其他模块再次提供它?你能显示正在导入你的ConfigModule resp的模块吗?使用你的ConfigService?

标签: nestjs


【解决方案1】:

考虑到我们关于不和谐的讨论,你是providing ConfigService 在你的 AppModule 而 也是 importing ConfigModule 这导致 Nest 认为它需要重新实例化ConfigService 但没有 filePath 构造函数变量。

从AppModule 的providers 数组中删除ConfigService 将解决问题。

【讨论】:

    猜你喜欢
    • 2019-05-13
    • 2020-09-21
    • 2019-12-14
    • 2019-12-23
    • 2019-08-15
    • 2021-09-08
    • 2020-12-30
    • 2020-04-22
    相关资源
    最近更新 更多