Home > OS >  Nest Error - ERROR [ExceptionHandler] Nest can't resolve dependencies of the AuthService (?, Jw
Nest Error - ERROR [ExceptionHandler] Nest can't resolve dependencies of the AuthService (?, Jw

Time:01-31

Issue with JWT service I'm trying to use for user auth

ERROR [ExceptionHandler] Nest can't resolve dependencies of the AuthService (?, JwtService). Please make sure that the argument UsersService at index [0] is available in the AuthModule context.

Here is the auth.module

import { Module } from "@nestjs/common"
import { JwtModule } from '@nestjs/jwt';
import { UsersModule } from "../users/users.module";
import { AuthService } from "./auth.service"
import { PassportModule } from "@nestjs/passport";
import { AuthController } from './auth.controller';
import { MongooseModule } from "@nestjs/mongoose"
import { UserSchema } from "users/schemas/users.schema";
import { LocalStrategy } from './local.auth';

@Module({
  imports: [UsersModule, PassportModule, JwtModule.register({
    secret: 'secretKey',
    signOptions: { expiresIn: '60s' },
  }), MongooseModule.forFeature([{ name: "Users", schema: UserSchema }])],
  providers: [AuthService, LocalStrategy],
  controllers: [AuthController],
})
export class AuthModule { }

Here is the App.module

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from './users/users.module';
import { MongooseModule } from '@nestjs/mongoose';
import { KpiModule } from './kpi/kpi.module';
import { AuthModule } from './auth/auth.module';

@Module({
  imports: [    
    MongooseModule.forRoot('mongodb://127.0.0.1:27017/vstkpi?readPreference=primary'),
    UsersModule,
    KpiModule,
    AuthModule
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

and userService

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { User } from 'users/interfaces/users.interface';

@Injectable()
export class UsersService {
    constructor(@InjectModel('Users') private readonly userModel: Model<User>) {}

    //Get all users
    async getUsers(): Promise<User[]> {
        const users = await this.userModel.find().exec();
        return users
    }

    //Get single user
    async getUser(query: object ): Promise<User> {
        return this.userModel.findOne(query);
    }

    async addUser(
        firstname: string, 
        lastname: string, 
        jobtitle: string, 
        startdate: string,
        password: string,
        email: string): Promise<User> {
        return this.userModel.create({
            firstname,
            lastname,
            jobtitle,
            startdate,
            password,
            email
        });
    }

}

I have tried to reorder imports and such but its not firing past that error

added userModule

import { Module } from '@nestjs/common';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import { UserSchema } from 'users/schemas/users.schema';
import { MongooseModule } from '@nestjs/mongoose';

@Module({
    imports: [
        MongooseModule.forFeature([{ name: 'Users', schema: UserSchema }])
    ],
    providers: [UsersService],
    controllers: [UsersController]
})
export class UsersModule { }

CodePudding user response:

you didn't exported UsersService from UsersModule module, so that provider is not visible to other modules.

Just add this to UsersModule config:

exports: [UsersService]
  • Related