Reputation: 1575
I have read the article on the nestjs docs about hierarchical inject. But I am having problems implementing it. Currently, I have two modules.
AuthModule
is the module that is importing the UserModule
. UserModule
contains a service that is used in other modules (where it works correctly).
auth.module.ts
import * as passport from 'passport';
import {
Module,
NestModule,
MiddlewaresConsumer,
RequestMethod,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import {JwtStrategy} from './jwt.straegy';
import {UserModule} from '../shared/user/user.module';
@Module({
imports: [UserModule],
components: [AuthService, JwtStrategy],
exports: [AuthService]
})
export class AuthModule implements NestModule {
public configure(consumer: MiddlewaresConsumer) {
consumer
.apply(passport.authenticate('jwt', { session: false }))
.forRoutes({ path: '/cats', method: RequestMethod.ALL });
}
}
user.module.ts
import {Module} from '@nestjs/common';
import {MongooseModule} from '@nestjs/mongoose';
import {UserSchema} from '../../schema/user.schema';
import {UserService} from './user.service';
@Module({
imports: [
MongooseModule.forFeature([{ name: 'UserInterface', schema: UserSchema }])
],
components: [UserService],
exports: [UserService]
})
export class UserModule {}
The problem arises when the nestjs injects UserService into AuthService. The following error is encountered. Removing the service from the constructor works. The weird thing is that the UserService works in other Modules.
Error: Nest can't resolve dependencies of the AuthService (?). Please verify whether [0] argument is available in the current context.
auth.service.ts
import * as jwt from 'jsonwebtoken';
import { Component } from '@nestjs/common';
import {UserService} from '../shared/user/user.service';
@Component()
export class AuthService {
constructor(private readonly userService: UserService) {}
async createToken(username) {
const expiresIn = 60 * 60, secretOrKey = 'i am fake!';
const user = {username};
const token = jwt.sign(user, secretOrKey, {expiresIn});
return {
expires_in: expiresIn,
access_token: token
};
}
async validateUser(signedUser): Promise<boolean> {
return !!(await this.userService.findByUsername(signedUser.username));
}
}
Upvotes: 1
Views: 2144
Reputation: 605
Thats weird, i do have almost the same code that works, one think i noticed, i am not sure if thats the issue here but you might use the authService in anther module? if so you might need to re-export the usersModule in the authModule so your current scope will have it:
in authModule:
exports: [UserModule, AuthService]
(Couldnt show full example since stackoverflow woke up and the wrong foot and wont let me add a code block)
If this change works, you might have "imported"(addded to components array) authService to anther module without importing the whole authModule, if you can provide github repo with all the code it could be more easer.
remember, authService is now part of authModule and should never be added directly to anther module but only trough the "import" array, if thats not your case, you can provide github repo with all the code it could be more easer to help
Upvotes: 1