
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 中是否错写了控制器和提供器的名称,将其添加到 imports 数组中。确保 bookentity 模块已被 appmodule 导入。检查 bookentityrepository 是否正确注入到 bookservice 中。
以下是修改后的代码示例:
app.module.ts
Poixe AI
统一的 LLM API 服务平台,访问各种免费大模型
75 查看详情
@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, ) { } async findAll(): Promise { return await this.bookRepository.find(); }}
进行上述更改后,报错应不再出现。如果仍遇到问题,请确保项目中所有依赖项都已正确安装。
以上就是Nest 使用 TypeORM 报错“Nest can’t resolve dependencies of the BookService…”,如何解决?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/739100.html
微信扫一扫
支付宝扫一扫