Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injection scopes: Scope.REQUEST doesn't work on middlewares #2648

Closed
shangsinian opened this issue Jul 25, 2019 · 5 comments
Closed

Injection scopes: Scope.REQUEST doesn't work on middlewares #2648

shangsinian opened this issue Jul 25, 2019 · 5 comments

Comments

@shangsinian
Copy link

Bug Report

Current behavior

Currently,I can‘t get same instance in two different middleware

Input Code

import { NestFactory } from '@nestjs/core';

import { 
  Module, 
  Controller, 
  Get, 
  Scope, 
  Inject,
  Injectable, 
  NestMiddleware, 
  MiddlewareConsumer,
  NestModule 
} from '@nestjs/common';

@Injectable()
export class MyProvider {

  public _id

  constructor() {
    this._id = parseInt(Math.round(Math.random()*1000000).toString());
  }
}

@Injectable()
export class Middleware2 implements NestMiddleware {

  constructor(
    @Inject("MY_PROVIDER") private readonly myProvider: MyProvider,
  ){}

  use(req: any, res: any, next: Function) {
    console.log(this.myProvider._id)
    next()
  }
}

@Injectable()
export class Middleware1 implements NestMiddleware {

  constructor(
    @Inject("MY_PROVIDER") private readonly myProvider: MyProvider,
  ){}

  use(req: any, res: any, next: Function) {
    console.log(this.myProvider._id)
    next()

  }
}

@Controller("api")
export class AppController {

  @Get(":id")
  getHello(): string {
    return "Hello World";
  }
}


@Module({
  imports: [],
  controllers: [AppController],
  providers: [{
    provide: "MY_PROVIDER",
    scope: Scope.REQUEST,
    useClass: MyProvider,
  }],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(Middleware1, Middleware2)
      .forRoutes('*');
  }
}

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();

Expected behavior

Get same provider instance in two or more middlewares

Environment


Nest version: 6.5.3
 
For Tooling issues:
- Node version: 12.6.0  
- Platform: Mac 

@shangsinian shangsinian added the needs triage This issue has not been looked into label Jul 25, 2019
@kamilmysliwiec
Copy link
Member

Please, provide a minimal repository which reproduces your issue.

@kamilmysliwiec kamilmysliwiec added needs clarification and removed needs triage This issue has not been looked into labels Jul 25, 2019
@shangsinian
Copy link
Author

OK, Here is the demo repository, https://github.com/shangsinian/nest-middleware-test

@kamilmysliwiec
Copy link
Member

Fixed in 6.6.0 :)

@shangsinian
Copy link
Author

@kamilmysliwiec Great, it's working fine now, Thanks!👍👍

@lock
Copy link

lock bot commented Nov 26, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants