【问题标题】:NestJS - Injected service is undefined in the constructorNestJS - 在构造函数中未定义注入的服务
【发布时间】:2018-10-11 04:29:51
【问题描述】:

根据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
    }
}

【问题讨论】:

  • 你用的是什么版本?你能分享你的tsconfig 文件吗?
  • @KamilMyśliwiec 好的,我已经用我的依赖项和 tsconfig 更新了我的问题

标签: javascript node.js dependency-injection nestjs


【解决方案1】:

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

【讨论】:

  • 哦哇哦O_o 我不知道这是什么,但它解决了问题。我永远不会自己发现。谢谢卡米尔,你就是那个人。
  • 今天有相同(相似)的问题,必须将以下内容添加到我的 tsconfig.json “experimentalDecorators”:true,“declaration”:true,“removeComments”:true,“emitDecoratorMetadata”:true
【解决方案2】:

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

【讨论】:

  • 你节省了我的时间。谢谢!
  • 哈哈,我喜欢这种情况。这就像一个可能存在相邻问题的清单。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 2020-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多