{"id":7241,"date":"2024-11-14T09:14:43","date_gmt":"2024-11-14T01:14:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7241\/"},"modified":"2024-11-14T09:14:43","modified_gmt":"2024-11-14T01:14:43","slug":"nest-%e4%b8%ad-typeorm-%e7%9a%84%e6%ad%a3%e7%a1%ae%e4%bd%bf%e7%94%a8%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bc%9a%e5%87%ba%e7%8e%b0nest-cant-resolve-dependencies-of-the-bookservice","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7241\/","title":{"rendered":"Nest \u4e2d TypeOrm \u7684\u6b63\u786e\u4f7f\u7528\uff1a\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u201cNest can&#8217;t resolve dependencies of the BookService\u201d\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173062929015917.jpg\" class=\"aligncenter\" title=\"Nest \u4e2d TypeOrm \u7684\u6b63\u786e\u4f7f\u7528\uff1a\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u201cNest can&#8217;t resolve dependencies of the BookService\u201d\uff1f\u63d2\u56fe\" alt=\"Nest \u4e2d TypeOrm \u7684\u6b63\u786e\u4f7f\u7528\uff1a\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u201cNest can&#8217;t resolve dependencies of the BookService\u201d\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>nest \u4e2d typeorm \u7684\u6b63\u786e\u4f7f\u7528<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 nest \u65f6\uff0c\u51fa\u73b0\u9519\u8bef\u6d88\u606f\u201cnest can&#8217;t resolve dependencies of the bookservice\u201d\uff0c\u63d0\u793a bookentityrepository \u6ca1\u6709\u5728 appmodule \u4e2d\u6ce8\u518c\u3002\u4ee5\u4e0b\u662f\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<p>app.module.ts\uff1a<\/p>\n<pre>@module({\n    imports: [\n        typeormmodule.forroot({\n            \/\/ \u6570\u636e\u5e93\u914d\u7f6e\n        }),\n        bookmodule,\n    ]\n})\nexport class appmodule {}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>book.module.ts\uff1a<\/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>book.service.ts\uff1a<\/p>\n<pre>import { InjectRepository } from '@nestjs\/typeorm';\nimport { Repository } from 'typeorm';\n\nexport class BookService {\n    constructor(\n        @InjectRepository(BookEntity)\n        private readonly bookRepository: Repository&lt;BookEntity&gt;,\n    ) {}\n\n    \/\/ \u4e1a\u52a1\u903b\u8f91\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u89e3\u51b3\u65b9\u5f0f<\/strong><\/p>\n<ol>\n<li>\u79fb\u9664 app.module.ts \u4e2d\u5197\u4f59\u7684 providers \u548c controllers \u4ee3\u7801\u3002<\/li>\n<li>\u786e\u4fdd bookentity \u5728 typeormmodule.forfeature \u4e2d\u88ab\u5f15\u7528\u3002<\/li>\n<\/ol>\n<p>\u5982\u6b64\u4fee\u6539\u540e\uff0cnest \u4fbf\u53ef\u4ee5\u6b63\u786e\u89e3\u6790 bookservice \u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u6d88\u9664\u9519\u8bef\u63d0\u793a\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fNest \u4e2d TypeOrm \u7684\u6b63\u786e\u4f7f\u7528\uff1a\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u201cNest can&#8217;t resolve dependencies of the BookService\u201d\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 \u4e2d typeorm \u7684\u6b63\u786e\u4f7f\u7528 \u5728\u4f7f\u7528 nest \u65f6\uff0c\u51fa\u73b0\u9519\u8bef\u6d88\u606f\u201cnest can&#8217;t resolve dependencies of the bookservice\u201d\uff0c\u63d0\u793a bookentityrepository \u6ca1\u6709\u5728 appmodule \u4e2d\u6ce8\u518c\u3002\u4ee5\u4e0b\u662f\u89e3\u51b3\u65b9\u6cd5\uff1a \u4ee3\u7801\u793a\u4f8b app.module.ts\uff1a @module({ imports: [ typeormmodule.forroot({ \/\/ \u6570\u636e\u5e93\u914d\u7f6e }), bookmodule, ] }) export class appmodule {} \u767b\u5f55\u540e\u590d\u5236 book.module.ts\uff1a @module({ imports: [typeormmodule.forfeature([bookentity])], controllers: [bookcontroller], providers: [bookservice], exports: [bookservice] }) export class bookmodule {} \u767b\u5f55\u540e\u590d\u5236 book.service.ts\uff1a import { InjectRepository } [&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-7241","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7241","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=7241"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7241\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}