Can't understand modules. Service imports don't work.
See original GitHub issueI get that I should ask on StackOverflow, but it’s a weird place where none of my questions ever get answered, aside from myself.
The documentation is lacking for someone like me, mentally challenged.
I cannot get any of this to work either…
app.module.ts
@Module({
imports: [
HttpModule,
AuthenticationModule,
UserModule,
],
controllers: [AppController],
})
export class AppModule {}
user.module.ts
@Module({
providers: [UserService],
exports: [UserService],
})
export class UserModule {}
auth.module.ts
@Module({
imports: [
HttpModule,
UserModule,
PassportModule,
JwtModule.register({ secret: process.env.JWT_SECRET }),
],
providers: [AuthenticationService, LocalStrategy],
controllers: [AuthenticationController],
exports: [AuthenticationService],
})
export class AuthenticationModule {}
auth.service.ts
@Injectable()
export class AuthenticationService implements IAuthenticationService {
constructor(
protected readonly userService: IUserService,
protected readonly jwtService: JwtService,
) {}
/**
* Attempts to authenticate a user with a given email and password combination.
*
* @param email
* @param password
*/
public async authenticate(email: string, password: string): Promise<Session> {
try {
const user = await this.userService.getAuthenticationDetails(email);
if (isNil(user)) {
throw new Error(NOT_FOUND_ERR);
}
if (!comparePassword(password, user.password, user.salt)) {
throw new Error(MISMATCH_ERR);
}
return sessionFactory(user._id.toString(), user.email, user.userType);
} catch (err) {
throw err;
}
}
/**
* Creates a JWT from a session
*
* @param session Session object to produce the JWT out of
*/
public signJWT(session: ISession): string {
return this.jwtService.sign(session);
}
}
Now, when I try to run npm start
I get this:
Nest can't resolve dependencies of the AuthenticationService (?, JwtService). Please make sure that the argument at index [0] is available in the AuthenticationModule context. +16ms
The docs say that I just need to import the module. Yet nothing is working. So are the docs wrong? What do I do here? Do I need to set authentication module providers to have UserService
in there? If so, what’s the point of modules in that case if I can dump EVERYTHING into the main app.module.ts
instead? Because this seems to be way easier.
Edit:
is the auth.module.ts
supposed to look like this then?
@Module({
imports: [
HttpModule,
UserModule,
PassportModule,
JwtModule.register({ secret: process.env.JWT_SECRET }),
],
// Added User Service bellow at index [1]
providers: [AuthenticationService, UserService, LocalStrategy],
controllers: [AuthenticationController],
exports: [AuthenticationService],
})
export class AuthenticationModule {}
Doesn’t it beat the purpose of modules?
Edit 2:
Well that didn’t work.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (3 by maintainers)
@TheImpressionist more on the interface vs class thing.
From the docs:
Plus further elaboration #1228
tl;dr: Not a Nest problem, it is a TS/JS problem because interfaces do not exist on run-time.
Apparently the constructor needs to look like THIS:
Instead of:
Means I cannot have interfaces in the constructor. Weird flex, but ok.