Nestjs dependency injection Unable to resolve dependencies error

Asked

Viewed 215 times

0

I made a simple API using Nestjs to learn. Now I’m making a more complex modularizing, but I had this dependency error:

Error: Nest can't resolve dependencies of the UserRepository (?). Please make sure that the argument UsersModel at index [0] is available in the UserRepository context.

Potential solutions:
- If UsersModel is a provider, is it part of the current UserRepository?
- If UsersModel is exported from a separate @Module, is that module imported within    UserRepository?
@Module({
   imports: [ /* the Module containing UsersModel */ ]
})

Userrepository.ts

import { Model } from 'mongoose'
import { User } from '../domain/user.schema';
import { InjectModel } from '@nestjs/mongoose';

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

   async create(userRegisterDto): Promise<User> {
      const createdUser = new this.userModel(userRegisterDto);
      return createdUser.save();
   }
}

Userservice.ts

import { Injectable } from "@nestjs/common";
import { UserRepository } from "../repository";

@Injectable()
export class UserService {
   constructor(private readonly repository: UserRepository) { }

   public async save(user){
      this.repository.create(user)
   }
}

Usermodule.ts

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { UserController } from './controller';
import { UserSchema } from './domain';
import { UserRepository } from './repository';
import { UserService } from './service';

@Module({
   imports: [
      MongooseModule.forFeature([{ name: 'Users', schema: UserSchema }]),
      UserRepository
   ],
   controllers: [UserController],
   providers: [UserService],
   exports: [UserService]
 })

 export class UserModule { }
  • 1

    Unfortunately there is still very little information from Nestjs, When it comes to the Portuguese language, I believe that documentation is the best way. I tried to mess with him already and I ran into this difficulty of information shortage!

  • Userrepository should be on the providers list

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.