nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.89k stars 7.56k forks source link

Cant inject desired dependency #360

Closed zelazna closed 6 years ago

zelazna commented 6 years ago

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[x] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I've a weird issue, Nest.js generate an error if i imported the services dependencies in the module.ts. I try to mix 2 services together, the UsersService and the AuthService

The AuthService dropping the error :

export class AuthService {
   constructor(private readonly usersService: UsersService) { } 
}

The AuthModule :

@Module({
    controllers: [AuthController],
    components: [AuthService, JwtStrategy],
    imports: [UsersModule],
    exports: [AuthService, JwtStrategy],
})
export class AuthModule { }

The UsersModule:

@Module({
    controllers: [UsersController],
    components: [UsersService],
    imports: [TypeOrmModule.forFeature([User])],
    exports: [UsersService],
})
export class UsersModule { }

At last the generated error :

 Error: Nest can't resolve dependencies of the AuthService (?). Please 
 verify whether [0] argument is available in the current context.

I already tried to export import the TypeOrmModule by it's not currently working, any idea ? thanks by advance !

Expected behavior

The dependency is injected properly in the service

Minimal reproduction of the problem with instructions

here's the github

Environment


Nest version: 4.4.0


For Tooling issues:
- Node version: 8.9  
- Platform:  Mac

Others:

kamilmysliwiec commented 6 years ago

Hi @zelazna, You defined AuthService and JwtStrategy twice. They're needless in the CatsModule, let's remove them https://github.com/zelazna/Nest.js-Test/blob/master/src/cats/cats.module.ts

zelazna commented 6 years ago

Thanks @kamilmysliwiec, it works !! Is it an expected behaviour ?

kamilmysliwiec commented 6 years ago

Definitely. Why did you define them twice? 🙂

zelazna commented 6 years ago

i messed up :), i'm talking about the error dropped, wich was very confusing for me ( no offense, Nest.js is really awesome, keep going ! )

kamilmysliwiec commented 6 years ago

thanks! 😃

TotallWAR commented 6 years ago

I have a service and an event-gateway. They use each other with depency injection pattern. I had made according to examples forwardRef method.

Problem: each of them are created twice, they are not singletones.

events.module.ts

import {forwardRef, Module} from '@nestjs/common';
import {EventsGateway} from './events.gateway';
import {WidgetModule} from '../widget/widget.module';
import {WidgetService} from '../widget/widget.service';`

@Module({
    providers: [EventsGateway, WidgetService],
    imports: [forwardRef(() => WidgetModule)],
    exports: [EventsGateway],
})
export class EventsModule {
}

widget.module.ts

import {WidgetController} from './widget.controller';
import {UserService} from './user.service';
import {MiddlewaresConsumer} from '@nestjs/common/interfaces/middlewares';
import {LoggerMiddleware} from '../common/middlewares/logger.middleware';
import {BrainService} from './brain.service';
import {WidgetService} from './widget.service';
import {EventsModule} from '../events/events.module';
import {EventsGateway} from '../events/events.gateway';

@Module({
    controllers: [WidgetController],
    providers: [UserService, BrainService, WidgetService, EventsGateway],
    exports: [WidgetService, UserService, BrainService],
    imports: [forwardRef(() => EventsModule)],
})
export class WidgetModule implements NestModule {
    configure(consumer: MiddlewaresConsumer) {
        consumer.apply([LoggerMiddleware]).forRoutes(WidgetController);
    }
}

widget.service.ts

@Injectable()
export class WidgetService {
    constructor(private readonly userService: UserService,
                private readonly brainSerivce: BrainService,
                @Inject(forwardRef(() => EventsGateway))
                private readonly eventsGateway: EventsGateway) {
        console.log('WidgetService created');

    }
...
}

events.gateway.ts

@WebSocketGateway(8080)
export class EventsGateway {
    @WebSocketServer() server;

    constructor(
        @Inject(forwardRef(() => WidgetService))
        private readonly widgetService: WidgetService) {
        console.log('EventsGateway created');
    }
...
}
lock[bot] commented 4 years ago

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.