{"id":7237,"date":"2024-11-14T14:29:07","date_gmt":"2024-11-14T06:29:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7237\/"},"modified":"2024-11-14T14:29:07","modified_gmt":"2024-11-14T06:29:07","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:\/\/fwq.ai\/blog\/7237\/","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><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173062693673284.jpg\" class=\"aligncenter\" title=\"Nest \u4f7f\u7528 TypeORM \u62a5\u9519\u201cNest can&#8217;t resolve dependencies of the BookService&#8230;\u201d\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" alt=\"Nest \u4f7f\u7528 TypeORM \u62a5\u9519\u201cNest can&#8217;t resolve dependencies of the BookService&#8230;\u201d\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" \/><\/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 avlable 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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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>\u4ee5\u4e0a\u5c31\u662fNest \u4f7f\u7528 TypeORM \u62a5\u9519\u201cNest can&#8217;t resolve dependencies of the BookService&#8230;\u201d\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>nest\u5982\u4f55\u4f7f\u7528typeorm \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 avlable in the appmodule context.\u201d\u7684\u9519\u3002 \u4ee5\u4e0b\u662f\u5bfc\u81f4\u6b64\u95ee\u9898\u7684\u4e00\u4e9b\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6848\uff1a \u68c0\u67e5 app.module.ts \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 \u786e\u4fdd bookentity \u6a21\u5757\u5df2\u88ab appmodule \u5bfc\u5165\u3002 \u68c0\u67e5 bookentityrepository \u662f\u5426\u6b63\u786e\u6ce8\u5165\u5230 bookservice \u4e2d\u3002 \u4ee5\u4e0b\u662f\u4fee\u6539\u540e\u7684\u4ee3\u7801\u793a\u4f8b\uff1a app.module.ts @module({ imports: [typeormmodule.forroot({ type: &#8216;mysql&#8217;, host: &#8216;localhost&#8217;, port: 3306, username: &#8216;root&#8217;, password: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-7237","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=7237"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7237\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}