ChristmasFighters
ChristmasFighters

Reputation: 93

ERROR [ExceptionHandler] Nest can't resolve dependencies of the UsersService (?)

i'm playing around with nest a bit and i'm trying to build a module that i can publish on my private repo so that i can reuse it whenever i need it.

but i encounter this error:

ERROR [ExceptionHandler] Nest can't resolve dependencies of the UserRepository (?). Please make sure that the argument DataSource at index [0] is available in the TypeOrmModule context.

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

i've built the module using nest build and then npm link it to the main project.

if the module is inside the main project it works, the problem arise when it's in another repo.

here is my app.module

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule, User } from '@MY-PACKAGE-LINKED/user';

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      useFactory: () => ({
        type: 'mysql',
        host: 'localhost',
        port: 3306,
        username: 'root',
        password: 'root',
        database: 'test',
        entities: [User],
        synchronize: true,
      }),
    }),
    UsersModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule { }

here's my users.module

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
import { User } from './entities/user.entity';
import { UserRepository } from './users.repository';
@Module({
  imports: [TypeOrmModule.forFeature([User])],
  controllers: [UsersController],
  providers: [UserRepository, UsersService],
})
export class UsersModule { }

what i thought was passing in the app.module in the providers array the UsersService, like so

...
  controllers: [AppController],
  providers: [AppService, UsersService],
})
...

but by doing so i get another error (a different one, we are going places!)

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

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

what am i doing wrong here? i feel like it's a dumb thing, because it works if the module is on the same project, it only brakes when i try to have it another repo and link it

edit:

users.service

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './entities/user.entity';
import { CreateUserParams } from './utils/types/types';

@Injectable()
export class UsersService {
    constructor(@InjectRepository(User) private userRepository: Repository<User>) { }

    async findOne(username: string): Promise<User | undefined> {
        return this.userRepository.findOne({ where: { username } });
    }

    async findAll(): Promise<User[]> {
        return this.userRepository.find();
    }

    createUser(userDetails: CreateUserParams) {
        const newUser = this.userRepository.create({ ...userDetails, createdAt: new Date() });
        return this.userRepository.save(newUser);
    }
}

edit2: the problem seems to be that the connection does not get shared, and by doing so this problem occurs...

i'm trying to solve it, i hope it will work :p

SOLVED!

the problem is npm link...

i used https://github.com/wclr/yalc and everything works!!

Upvotes: 1

Views: 816

Answers (1)

ChristmasFighters
ChristmasFighters

Reputation: 93

the solution was easy.

the problem was npm link.

i used https://github.com/wclr/yalc and everything works!!

Upvotes: 0

Related Questions