{"id":201009,"date":"2025-05-06T12:51:42","date_gmt":"2025-05-06T04:51:42","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201009\/"},"modified":"2025-05-06T12:51:42","modified_gmt":"2025-05-06T04:51:42","slug":"nest-%e4%bd%bf%e7%94%a8-typeorm-%e6%8a%a5%e9%94%99nest-cant-resolve-dependencies-of-the-bookservice-%ef%bc%8c%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201009\/","title":{"rendered":"Nest \u4f7f\u7528 TypeORM \u62a5\u9519\u201cNest can\\&#8217;t resolve dependencies of the BookService&#8230;\u201d\uff0c\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Nest \u4f7f\u7528 TypeORM \u62a5\u9519\u201cNest can\\&#8217;t resolve dependencies of the BookService&#8230;\u201d\uff0c\u5982\u4f55\u89e3\u51b3\uff1f<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241125\/1732511926674408b6d1c59.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>nest\u5982\u4f55\u4f7f\u7528typeorm<\/strong><\/p>\n<p>\u5728nest\u6846\u67b6\u4e2d\u4f7f\u7528typeorm\u65f6\uff0c\u5e38\u5e38\u4f1a\u9047\u5230\u62a5\u201cnest can&#8217;t resolve dependencies of the bookservice (?). please make sure that the argument bookentityrepository at index [0] is available in the appmodule context.\u201d\u7684\u9519\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5bfc\u81f4\u6b64\u95ee\u9898\u7684\u4e00\u4e9b\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<ul>\n<li>\u68c0\u67e5 <strong>app.module.ts<\/strong> \u4e2d\u662f\u5426\u9519\u5199\u4e86\u63a7\u5236\u5668\u548c\u63d0\u4f9b\u5668\u7684\u540d\u79f0\uff0c\u5c06\u5176\u6dfb\u52a0\u5230 imports \u6570\u7ec4\u4e2d\u3002<\/li>\n<li>\u786e\u4fdd <strong>bookentity<\/strong> \u6a21\u5757\u5df2\u88ab <strong>appmodule<\/strong> \u5bfc\u5165\u3002<\/li>\n<li>\u68c0\u67e5 <strong>bookentityrepository<\/strong> \u662f\u5426\u6b63\u786e\u6ce8\u5165\u5230 <strong>bookservice<\/strong> \u4e2d\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u662f\u4fee\u6539\u540e\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<p><strong>app.module.ts<\/strong><\/p>\n<pre>@module({\n    imports: [typeormmodule.forroot({\n        type: 'mysql',\n        host: 'localhost',\n        port: 3306,\n        username: 'root',\n        password: 'root',\n        database: 'root',\n        entities: [__dirname + '\/**\/*.entity.{js,ts}'],\n        synchronize: true,\n        timezone: 'z',\n    }), bookmodule],\n})\nexport class appmodule { }<\/pre>\n<p><strong>book.module.ts<\/strong><\/p>\n<pre>@module({\n  imports: [typeormmodule.forfeature([bookentity])],\n  controllers: [bookcontroller],\n  providers: [bookservice],\n  exports: [bookservice]\n})\nexport class bookmodule { }<\/pre>\n<p><strong>book.service.ts<\/strong><\/p>\n<pre>import { InjectRepository } from \"@nestjs\/typeorm\";\nimport { Repository } from \"typeorm\";\nimport { BookEntity } from '.\/book.entity';\n\n@Injectable()\nexport class BookService {\n  constructor(\n    @InjectRepository(BookEntity)\n    private readonly bookRepository: Repository&lt;BookEntity&gt;,\n  ) { }\n\n  async findAll(): Promise&lt;BookEntity[]&gt; {\n    return await this.bookRepository.find();\n  }\n}<\/pre>\n<p>\u8fdb\u884c\u4e0a\u8ff0\u66f4\u6539\u540e\uff0c\u62a5\u9519\u5e94\u4e0d\u518d\u51fa\u73b0\u3002\u5982\u679c\u4ecd\u9047\u5230\u95ee\u9898\uff0c\u8bf7\u786e\u4fdd\u9879\u76ee\u4e2d\u6240\u6709\u4f9d\u8d56\u9879\u90fd\u5df2\u6b63\u786e\u5b89\u88c5\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aNest \u4f7f\u7528 TypeORM \u62a5\u9519\u201cNest can\\&#8217;t resolve dependencies of the BookService&#8230;\u201d\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Nest \u4f7f\u7528 TypeORM &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201009","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201009"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201009\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}