Nest 无法解析 TypeOrmModuleOptions 的依赖关系(?)

问题描述 投票:0回答:2

我有一个 typeorm.config.ts 文件和一个 app.module.ts 文件。

typeorm.config.ts

import { TypeOrmModuleOptions } from "@nestjs/typeorm";

export const typeORMConfig: TypeOrmModuleOptions = {
  type: "mysql",
  database: "abc",
  host: "abc",
  port: 1234,
  username: "root",
  password: "1234",
  logging: true,
  entities: ["dist/**/**.entity{.ts,.js}"],
  synchronize: true,
};

app.module.ts

import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { AuthModule } from "./auth/auth.module";
import { PostModule } from "./post/post.module";
import { typeORMConfig } from "./configs/typeorm.config";

@Module({
  imports: [
    TypeOrmModule.forRootAsync(typeORMConfig),
    AuthModule,
    PostModule,
  ],
})
export class AppModule {}

当我运行 npm run start 时,出现以下错误。

Nest can't resolve dependencies of the TypeOrmModuleOptions (?). Please make sure that the argument dependency at index [0] is available in the TypeOrmCoreModule context.
Potential solutions:
- If dependency is a provider, is it part of the current TypeOrmCoreModule?
- If dependency is exported from a separate @Module, is that module imported within TypeOrmCoreModule?
  @Module({
    imports: [ /* the Module containing dependency */ ]
  })

我找不到该错误的原因。 我究竟做错了什么? 我需要帮助。

typescript nestjs typeorm
2个回答
4
投票

看起来你的初始化方式有问题

TypeOrmModule

共有三个选项

forRoot
forFeature
forRootAsync

查看您提供的示例,您希望使用

forRootAsync
异步加载配置,它需要
TypeOrmModuleAsyncOptions
作为参数

在这种情况下,您的

TypeOrmModule
绑定应如下所示

TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: DatabaseConfiguration,
      inject: [ConfigService],
   })

其中

DatabaseConfiguration
应该是一个返回以下函数的异步函数

export default (configService: ConfigService): TypeOrmModuleOptions => {
  const options: TypeOrmModuleOptions = {
    type: 'mysql',
    host: configService.get('database.host'),
    port: +configService.get<number>('database.port'),
    username: configService.get('database.user'),
    password: configService.get('database.password'),
    database: configService.get('database.name'),
    entities: [/** entities here **/],
    synchronize: false,
  };
  return options;
};

要从环境变量或其他秘密管理器加载数据库凭据和其他变量,您应该使用

ConfigService
 中的 
ConfigModule

如果您想同步加载数据库凭据和其他详细信息,那么您应该使用

forRoot
,它需要
TypeOrmModuleOptions
作为参数,如下所示

TypeOrmModule.forRoot({ \** JSON here **\})

0
投票

可能有助于解决此问题。 出现这种错误是因为缺少“imports: [ConfigModule],”。

import { Module } from "@nestjs/common";
import { ConfigModule,ConfigService } from "@nestjs/config";
import { TypeOrmModule } from "@nestjs/typeorm";


@Module({
    imports:[
        TypeOrmModule.forRootAsync({
            imports: [ConfigModule],
            inject: [ConfigService],
            useFactory:async (configService : ConfigService)=>({
                type: 'postgres',
                host: configService.get('DATABASE_HOST'),
                port: parseInt(configService.get('DATABASE_PORT')),
                username: configService.get('DATABASE_USERNAME'),
                password: configService.get('DATABASE_PASSWORD'),
            }),
           
        }),
        
    ],
})
© www.soinside.com 2019 - 2024. All rights reserved.