src/contact/contact.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ContactController } from './contact.controller';
import { Contact } from './contact.entity';
import { ContactRepository } from './contact.repo';
import { ContactService } from './contact.service';
@Module({
imports: [
TypeOrmModule.forFeature([
Contact,
]),
],
controllers: [ContactController],
providers: [ContactService, ContactRepository],
})
export class ContactModule {}
src/app.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { getMetadataArgsStorage } from 'typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ContactModule } from './contact/contact.module';
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'sqlite',
database: 'db',
entities: getMetadataArgsStorage().tables.map(tbl => tbl.target),
synchronize: true,
}),
ContactModule
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
npm 新开始:dev
出现这种错误,我尝试在互联网上找到可能的所有解决方案,但我不知道我犯了什么错误,我收到了类似的错误。
巢-v (8.1.6)
ERROR [ExceptionHandler] Nest can't resolve dependencies of the TypeOrmCoreModule (TypeOrmModuleOptions, ?). Please make sure that the argument ModuleRef at index [1] is available in the TypeOrmCoreModule context.
Potential solutions:
- If ModuleRef is a provider, is it part of the current TypeOrmCoreModule?
- If ModuleRef is exported from a separate @Module, is that module imported within TypeOrmCoreModule?
@Module({
imports: [ /* the Module containing ModuleRef */ ]
})
我遇到这个问题是因为 typeorm 安装错误。
对我有用:
1-删除node_module
2- 运行
npm i
npm install --save @nestjs/typeorm typeorm mysql2
。
当我从全局可用的
TypeOrmCoreModule
切换到自定义 ConfigService
时,我遇到了 EnvConfigService
的错误。在这种情况下,例如,我需要 import
自定义 EnvConfigService
所在的模块;如下粗体语句所示:
const typeOrmModule = TypeOrmModule.forRootAsync({
imports: [EnvConfigModule],
inject: [EnvConfigService],
useFactory: (config: EnvConfigService) => {
return {
type: 'postgres',
host: config.postgresHost,
port: config.postgresPort,
database: config.postgresDatabaseName,
username: config.postgresUsername,
password: config.postgresPassword,
synchronize: true,
entities: [User, PasswordRecovery]
}
}
})
当我注入
import
时,不需要ConfigService
语句,因为它是全局可访问的。
确保 monorepo 项目的不同包具有相同的版本,特别是以下库:
"@nestjs/common": "^10.3.7",
"@nestjs/core": "^10.3.7",
"@nestjs/platform-express": "^10.3.7",