Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJS - Injected service is undefined in the constructor

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

So :

  • 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??

EDIT - here are my dependencies :

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

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
    }
}
like image 755
Jeremy Thille Avatar asked May 01 '18 04:05

Jeremy Thille


2 Answers

The "emitDecoratorMetadata": true is missing in your tsconfig.json file.

like image 177
Kamil Myśliwiec Avatar answered Oct 14 '22 20:10

Kamil Myśliwiec


Just stumbled across the same issue in one of my providers - the problem was that I forgot to annotate the provider with an @Injectable() decorator after I have added it to the respective module.

like image 24
Georgi Stoimenov Avatar answered Oct 14 '22 22:10

Georgi Stoimenov