I am having an issue with implementing MongoDB in my Nest.js project. Despite what I believe to be a correct installation, I keep encountering the following error:
Nest can't resolve dependencies of the AuthService (SessionRepository, ?). Please ensure that the argument LogRepository at index [1] is available in the AuthModule context.
Below is a snippet of the relevant code:
// app.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AuthModule } from '@auth/auth.module';
import { MongooseModule } from '@nestjs/mongoose';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: `../../.env.${process.env.NODE_ENV}`
}),
MongooseModule.forRoot(process.env.MONGO_DB_LOGS),
AuthModule
]
})
export class AppModule {}
Authentication module code:
// auth.module.ts
import { Module } from '@nestjs/common';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { SequelizeModule } from '@nestjs/sequelize';
import { Session } from '@models/session.model';
import { MongooseModule } from '@nestjs/mongoose';
import { Log, LogSchema } from '@mongo-schemas/log.schema';
@Module({
providers: [AuthService],
exports: [AuthService],
controllers: [AuthController],
imports: [
MongooseModule.forFeature([{ name: 'Log', schema: LogSchema }]),
]
})
export class AuthModule {}
Mongoose schema used in the project:
// log.schema.ts
import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose';
@Schema()
export class Log {
@Prop()
message: string;
}
export const LogSchema = SchemaFactory.createForClass(Log);
Code for the authentication service:
import { Log } from '@mongo-schemas/log.schema';
import { Model } from 'mongoose';
@Injectable()
export class AuthService {
constructor(
@InjectModel(Session) private readonly sessionRepository: typeof Session,
@InjectModel(Log) private readonly logsRepository: Model<Log>
) {}
...
Thank you for your assistance!
PS. Note that the error mentions LogRepository
, whereas it should be LogsRepository
.