最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

NestJS

旗下网站admin35浏览0评论

NestJS

NestJS

本文介绍了NestJS-注入的服务在构造函数中未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

根据文档,我在控制器的构造函数中注入了服务,但结果是undefined.

As per the documentation, I inject a service in a controller's constructor, but it turns out as undefined.

processScraped.controller.ts

import { Controller, Post, Body } from '@nestjs/common';import { ProcessScrapedService } from "./processScraped.service"console.log(`\nController - ProcessScrapedService = `, ProcessScrapedService) // logs : class ProcessScrapedService { ......@Controller('processScraped')export class ProcessScrapedController { constructor(private readonly pss: ProcessScrapedService) { console.log(`constructor - pss = `, pss) // logs : undefined (Should not !) console.log(`constructor - this.pss = `, this.pss) // logs : undefined (Should not !) } @Post() async processScraped(@Body() body) { console.log(`processScraped - this.pss = `,this.pss) // logs : undefined (Should not !) return this.pss.processScraped(body) // TypeError: Cannot read property 'processScraped' of undefined }}

所以:

该服务存在

The service exists

在导入后已将其正确导入并记录为服务

It's imported and logged correctly as a service after import

当我将其注入控制器时,它是未定义的.

When I inject it in my controller, it's undefined.

问题可能出在服务定义中吗?

Maybe the problem is in the service definition?

processScraped.service.ts

import { Component } from '@nestjs/common';@Component()export class ProcessScrapedService { async processScraped(body) { // Some logic here return }}

...也许在模块中?

... Or maybe in the module?

processScraped.module.ts

import { Module } from '@nestjs/common';import { ProcessScrapedController } from './processScraped.controller';import { ProcessScrapedService } from './processScraped.service';console.log(`\Module - nProcessScrapedService = `, ProcessScrapedService) // logs : class ProcessScrapedService { ......@Module({ controllers: [ProcessScrapedController], components: [ProcessScrapedService],})export class ProcessScrapedModule { }

我真的看不到我在做什么错了吗?

I really can't see what I'm doing wrong here??

编辑-这是我的依赖项:

"dependencies": { "@nestjs/common": "^4.5.9", "@nestjs/core": "^4.5.10", "@nestjs/microservices": "^4.5.8", "@nestjs/mongoose": "^3.0.1", "@nestjs/testing": "^4.5.5", "@nestjs/websockets": "^4.5.8", "@types/mongoose": "^5.0.9", "bluebird": "^3.5.1", "dotenv": "^5.0.1", "elasticsearch": "^14.2.2", "express": "^4.16.3", "mongoose": "^5.0.16", "mongoose-elasticsearch-xp": "^5.4.1", "reflect-metadata": "^0.1.12", "rxjs": "^5.5.6", "shortid": "^2.2.8" }, "devDependencies": { "@types/node": "^8.0.0" }

和我的tsconfig.json:

and my tsconfig.json :

{ "compilerOptions": { "target": "ES2017", "module": "commonjs", "lib": [ "dom", "es2017" ], "outDir": "../../dist/server", "removeComments": true, "strict": true, "noImplicitAny": false, "typeRoots": [ "node_modules/@types" ], "types": [ "node" ], "experimentalDecorators": true }}

推荐答案

tsconfig.json文件中缺少"emitDecoratorMetadata": true.

NestJS

发布评论

评论列表(0)

  1. 暂无评论