#Can't Resolve controller dependencies - What did I do wrong ? need help

43 messages · Page 1 of 1 (latest)

vagrant otter
#

Hi everyone ! I have some trouble trying to import a module to my appModule and I can't find the solution to an error with controlle dependencies, I've searched on the docs for a long times and other sites like stackOverflow and did not find and answer.

Here is my problem:

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

Here is the provider.module.ts :

import { Module } from "@nestjs/common";
import { PrismaService } from "src/prisma.service";
import { ProviderController } from "./provider.controller";
import { ProviderService } from "./provider.service";

@Module({
    imports: [],
    controllers: [ProviderController],
    providers: [ProviderService, PrismaService],
    exports: [ProviderService]
})
export class ProviderModule {}
#

Here the provider.service.ts :

import { Injectable } from "@nestjs/common";
import { Provider, Prisma} from "@prisma/client";
import { PrismaService } from "src/prisma.service";

@Injectable()
export class ProviderService {
    constructor(private prisma: PrismaService) {}

    async updateUserToken(userID: number, providerName: string, accessToken: string = "null", refreshToken: string) {
        const userProviders = await this.getUserProviders({where: {userID : userID, Name : providerName}})
        const provider = userProviders[0]
        if (provider) {
            await this.prisma.user.update({
                where: {ID: userID, },
                data: {
                    Providers: {
                        update: {where: {ID : provider.ID} , data: {Name: providerName, accessToken: accessToken, refreshToken: refreshToken}}
                    }
                }
            })
        } else {
            await this.prisma.user.update({
                where: {ID: userID},
                data: {
                    Providers: {
                        create: {Name: providerName, accessToken: accessToken, refreshToken: refreshToken}
                    }
                }
            })
        }
    }

    async getUserProviders(params: {
    skip?: number;
    take?: number;
    cursor?: Prisma.ProviderWhereUniqueInput;
    where?: Prisma.ProviderWhereInput;
    orderBy?: Prisma.ProviderOrderByWithRelationInput;
  }): Promise<Provider[]> {
    const { skip, take, cursor, where, orderBy } = params;
    return this.prisma.provider.findMany({
      skip,
      take,
      cursor,
      where,
      orderBy,
    });
    }
}
#

the provider.controller.ts :

import { Controller, Get, Param, Post, Body, Put, Delete} from '@nestjs/common';
import { ProviderService } from './Provider.service';
import { Provider as ProviderModel} from '@prisma/client';

@Controller("Provider")
export class ProviderController {
    constructor(private providerService: ProviderService) {}

    @Get()
    async getProviders(): Promise<ProviderModel[]> {
        return this.providerService.getUserProviders({})
    }
}
#

and Finally my app.module.ts :

import { Module } from '@nestjs/common';
import { GoogleStrategy } from './Oauth/google.strategy';
import { OAuthService } from './Oauth/auth.service';
import { AuthController } from './Oauth/auth.controller';
import { HttpModule } from '@nestjs/axios';
import { ActionsService } from './actions/actions.service';
import { ReactionService } from './reactions/reaction.strategy';
import { TwitterActionsService } from "./actions/twitter/twitter.actions.service";
import { FlightService } from './actions/flight/flight.service';
import { GoogleActionsService } from './actions/google/google.actions.service';
import { AuthService } from './auth/auth.service';
import { LocalStrategy } from './auth/local.strategy';
import { PassportModule } from '@nestjs/passport';
import { JwtModule, JwtService } from '@nestjs/jwt';
import { JwtStrategy } from './auth/jwt.strategy';
import { AreaService } from './area/area.service';
import { ServicesService } from './services/services.service';
import { ProviderModule } from './providers/provider.module';
import { UserModule } from './user/user.module';

@Module({
  imports: [
    ConfigModule.forRoot(),
    HttpModule,
    PassportModule,
    JwtModule.register({secret: process.env.SECRET}),
    ProviderModule,
    UserModule
],
  controllers: [AppController, userController, AuthController],
  providers: [AppService, UserService, PrismaService,
              GoogleStrategy, LocalStrategy, OAuthService, ReactionService,
              ActionsService, TwitterActionsService,
              GoogleReactionsService, GoogleActionsService, DiscordReactionsService,
              FlightService, AuthService, JwtService, JwtStrategy, AreaService,
              ServicesService],
})
export class AppModule {}

I know that the appModule is a mess, i'm trying to clean up by adding modules.

Do you have any Idea on what I did wrong ?

#

I removed some of the imports on appModule to not take too much space

sage vortex
#

You need to show us the ProviderModule

#

Nvm

#

my bad

#

That’s odd
Your setup looks fine to me

#

Try rm -rf dist

vagrant otter
#

i've tried, it does not seem to be the problem sadly

sage vortex
#

🤔

vagrant otter
#

before trying to clean up, i had problems with the provider.service.ts file, i was simply adding provider.service.ts and provider.controller.ts in the appModule and I had the same kind of errors if it can help in any way

sage vortex
#

You don’t need to add them in the root module

vagrant otter
#

I didn't have a provider module when I did this first, so that's why i added them in the rootModule

#

also I'm using the provider Service in other services, that's why i exported the providerService

sage vortex
#

As long as you’ve imported the ProviderModule into them, that is fine

#

Did you have ProviderController somewhere else other than provider.module.ts?

vagrant otter
#

let me check

#

I've just searched but I don't

#

I could eventually share the code on github if that could help

sage vortex
#

can you run npm ls @nestjs/common

vagrant otter
#

here is the result

sage vortex
#

what is strange is that the error msg has the provider name in it, so it isn't a circular dep issue
And is pretty clear that ProviderService is registered in the same module as the ProviderController lives

#

so I don't see any reason for that error msg at all 🤔

#

please share the full code with us
along with the steps to reproduce the issue

vagrant otter
#

alright, I'm preparing the github repo

#

Ok, clone the repo then cd server && npm install && npm start

#

all the modules/controller/providers are in /server/src

sage vortex
#

oh, got it

#

it should be from 'provider.service' not from 'Provider.service'

#

try that

vagrant otter
#

I'll look

#

💀

sage vortex
#

I didn't manage to reproduce it due to missing env. vars

vagrant otter
#

Thank you SO MUCH for your help, i'm going to throw myself again ha ha, don't worry about the rest, that was really the dependencies part

#

you saved me from a reallllllly long time of looking for nothing but a auto generated import that did not updated on filename change 🤦‍♂️

sage vortex
#

also, I suggest you to add this config to your tsconfig:
"forceConsistentCasingInFileNames": true,

vagrant otter
#

Ok I'll add it, what does it do ?