Cannot inject request to request-scoped service with moduleRef resolve method
See original GitHub issueBug 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:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We’ve added the
registerRequestByContextId()
method to theModuleRef
in 7.3.0 release. ExampleDoes it work with GraphQL
Context
as well? I mean injecting a context:@Inject(CONTEXT)
and a request:@Inject(REQUEST)
.