NestJS - 构造函数中未定义注入的服务

时间:2018-05-01 04:26:27

标签: javascript node.js dependency-injection nestjs

根据the documentation,我在控制器的构造函数中注入了一个服务,但结果是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
    }
}

所以:

  • 服务存在

  • 导入后,它作为服务正确导入并记录

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

问题可能在于服务定义吗?

processScraped.service.ts

import { Component } from '@nestjs/common';

@Component()
export class ProcessScrapedService {
    async processScraped(body) {
        // Some logic here
        return
    }
}

......或者可能在模块中?

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 { }

我真的无法看到我在这里做错了什么?

编辑 - 这是我的依赖项:

"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:

{
    "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
    }
}

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

偶然发现了我的一个提供程序中的同一问题-问题是我在将其添加到各个模块后忘记使用@Injectable()装饰器对提供程序进行注释。