
nest如何使用typeorm
在nest框架中使用typeorm时,常常会遇到报“nest can't resolve dependencies of the bookservice (?). please make sure that the argument bookentityrepository at index [0] is available in the appmodule context.”的错。
以下是导致此问题的一些原因和解决方案:
以下是修改后的代码示例:
app.module.ts
@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...”,如何解决?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号