nest icon indicating copy to clipboard operation
nest copied to clipboard

Cannot inject into middleware

Open szkumorowski opened this issue 9 years ago • 8 comments

TypeError: Cannot set property 'oAuthServer' of undefined

@Component() export class OAuth2Server implements OnModuleInit

`@Middleware() export class OAuth2Middleware implements NestMiddleware {

private logger: Logger = LoggerFactory.getLogger(this.constructor.name);


constructor(@Inject('OAuth2Server') private readonly oAuthServer: OAuth2Server) {
}

async resolve(...args: any[]): Promise<ExpressMiddleware> {
    return (req, res, next) => {
        next();
    };
}

}`

export class AuthModule implements NestModule { public configure(consumer: MiddlewaresConsumer) { consumer .apply(OAuth2Middleware) .forRoutes(..) } }

szkumorowski avatar Nov 23 '17 11:11 szkumorowski

Hi @szkumorowski, We need a little bit more context. This part looks fine.

kamilmysliwiec avatar Nov 23 '17 12:11 kamilmysliwiec

Even so, there is problem with middlewares itself, actually any of examples in relation to middleware doesn't work except direct functions. Class resolve method is not executed.

` @ Middleware() export class LoggerMiddleware implements NestMiddleware {

constructor() {
    console.log('constructor');
}

resolve(...args: any[]): ExpressMiddleware {
    console.log('resolve');
    return (req, res, next) => {
        console.log('Request...');
        next();
    };
}

}`

export class AuthModule implements NestModule { configure(consumer: MiddlewaresConsumer): void { consumer.apply(LoggerMiddleware).forRoutes( { path: '/**', method: RequestMethod.POST }, ); } }

Server is not responding. Logs: constructor

Nothing more.

szkumorowski avatar Nov 23 '17 12:11 szkumorowski

I have tested 01-cats-app example right now, which has the same middleware. Everything is working fine, we still need more context

kamilmysliwiec avatar Nov 23 '17 12:11 kamilmysliwiec

tsconfig.json "target": "es5" - cause the problem

szkumorowski avatar Nov 23 '17 15:11 szkumorowski

I experienced this same issue, but was due to having multiple tsconfig files in my project -- front end uses es5 target and is named tsconfig.json so ts-node picked it up for my dev server. changing index.js to

require('ts-node').register({project: 'nestjs.tsconfig.json'});
require('./server');

with project being whatever your alternate tsconfig file's name.

EreckGordon avatar Nov 29 '17 02:11 EreckGordon

This PR https://github.com/nestjs/docs.nestjs.com/pull/43 adds a warning that target: es6 is required.

kamilmysliwiec avatar Feb 15 '18 23:02 kamilmysliwiec

As a new user I would expect a sentence in the Documentation / "First Steps" telling me what is needed in tsconfig.json like

{
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "target": "es6"
}

sebilasse avatar Dec 09 '18 08:12 sebilasse

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[bot] avatar Sep 24 '19 07:09 lock[bot]