Nest 使用 TypeORM 报错“Nest can't resolve dependencies of the BookService...”,如何解决?

nest如何使用typeorm
在nest框架中使用typeorm时,常常会遇到报“nest can't resolve dependencies of the bookservice (?). please make sure that the argument bookentityrepository at index [0] is *ailable in the appmodule context.”的错。
以下是导致此问题的一些原因和解决方案:
- 检查 a
pp.module.ts 中是否错写了控制器和提供器的名称,将其添加到 imports 数组中。 - 确保 bookentity 模块已被 appmodule 导入。
- 检查 bookentityrepository 是否正确注入到 bookservice 中。
以下是修改后的代码示例:
app.module.ts
码上飞
码上飞(CodeFlying) 是一款AI自动化开发平台,通过自然语言描述即可自动生成完整应用程序。
449
查看详情
@module({
imports: [typeormmodule.forroot({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'root',
database: 'root',
entities: [__dirname + '/**/*.entity.{js,ts}'],
synchronize: true,
timezone: 'z',
}), bookmodule],
})
export class appmodule { }book.module.ts
@module({
imports: [typeormmodule.forfeature([bookentity])],
controllers: [bookcontroller],
providers: [bookservice],
exports: [bookservice]
})
export class bookmodule { }book.service.ts
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { BookEntity } from './book.entity';
@Injectable()
export class BookService {
constructor(
@InjectRepository(BookEntity)
private readonly bookRepository: Repository<BookEntity>,
) { }
async findAll(): Promise<BookEntity[]> {
return await this.bookRepository.find();
}
}进行上述更改后,报错应不再出现。如果仍遇到问题,请确保项目中所有依赖项都已正确安装。
以上就是Nest 使用 TypeORM 报错“Nest can't resolve dependencies of the BookService...”,如何解决?的详细内容,更多请关注其它相关文章!

pp.module.ts 中是否错写了控制器和提供器的名称,将其添加到 imports 数组中。