question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot inject request to request-scoped service with moduleRef resolve method

See original GitHub issue

Bug Report

Current behavior

I cannot inject Request into service with moduleRef resolve method. The request object is always undefined. I’d like to inject request-scoped service into singleton-scoped controller.

Input Code

app.service.ts

import { Injectable, Scope, Inject } from '@nestjs/common';
import { REQUEST } from '@nestjs/core'

@Injectable({ scope: Scope.REQUEST })
export class AppService {

  constructor(@Inject(REQUEST) request: any) {
    console.log(request) //undefined
  }

  getHello(): string {
    return 'Hello World!';
  }
}

app.controller.ts

import { Controller, Get } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly moduleRef: ModuleRef) {}

  @Get()
  async getHello(): Promise<string> {
    return (await this.moduleRef.resolve(AppService)).getHello()
  }
}

app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Environment


"@nestjs/common": "^7.0.0",
"@nestjs/core": "^7.0.0",
"@nestjs/platform-express": "^7.0.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.5.4"

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
kamilmysliwieccommented, Jul 1, 2020

We’ve added the registerRequestByContextId() method to the ModuleRef in 7.3.0 release. Example

const contextId = ContextIdFactory.create();
this.moduleRef.registerRequestByContextId(YOUR_REQUEST_OBJECT, contextId);
const appService = await this.moduleRef.resolve(AppService, contextId);
0reactions
piotr-pawlowskicommented, Jul 2, 2020

Does it work with GraphQL Context as well? I mean injecting a context: @Inject(CONTEXT) and a request: @Inject(REQUEST).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Request-scoped service undefined in custom validaor
In particular, using the moduleRef.resolve() method, the scoped service is still coming in as undefined. I've been following these instructions ...
Read more >
Module reference | NestJS - A progressive Node.js framework
To dynamically resolve a scoped provider (transient or request-scoped), use the resolve() method, passing the provider's injection token as an argument.
Read more >
nestjs interceptor inject service - You.com | The AI Search ...
moduleRef.resolve should be able to correctly resolve service that depends on REQUEST , no matter whether the service was resolved elsewhere via different...
Read more >
Testing | NestJS 中文文档
Indeed, we aren't even using dependency injection (notice that we pass an ... ability to dynamically resolve scoped providers (transient or request-scoped).
Read more >
API with NestJS #68. Interacting with the application through ...
A good example is getting a service and calling its method. ... The resolve functionality resolves transient and request-scoped providers.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found