[ ] Regression
[x ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Trying to use my fileRepository: Repository<File>
in my FileService, but I get the following error:
[ExceptionHandler] Nest can't resolve dependencies of the FileService (?, +). Please verify whether [0] argument is available in the current context.
It should work
I had no problem on other modules, I have no idea why I get this error on this one.
file.module.ts
@Module({
imports: [TypeOrmModule.forFeature([File]), GlobalModule],
components: [FileService, GlobalService],
controllers: [FileController],
})
export class FileModule {}
file.service.ts
constructor(@InjectRepository(File)
private readonly fileRepository: Repository<File>,
private globalService: GlobalService) {
}
app.module.ts
@Module({
imports: [TypeOrmModule.forRoot({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: '',
database: 'thordetsv2',
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
synchronize: true,
}),
BugModule,
VersionModule,
FileModule
],
controllers: [AppController],
components: [],
})
bugfix
Nest version: 4.5.10
Could you show us entire files? Both file.module.ts
and file.service.ts
?
file.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import {FileService} from "./file.service";
import {FileController} from "./file.controller";
import {GlobalModule} from "../global/global.module";
import {GlobalService} from "../global/global.service";
import { File } from './file.entity';
@Module({
imports: [TypeOrmModule.forFeature([File]), GlobalModule],
components: [FileService, GlobalService],
controllers: [FileController],
})
export class FileModule {}
file.service.ts
import { Component } from '@nestjs/common';
import {File} from './file.entity';
import {GlobalService} from "../global/global.service";
@Component()
export class FileService {
oauth;
constructor(@InjectRepository(File) private readonly fileRepository: Repository<File>,
private globalService: GlobalService) {
this.oauth = this.globalService.initAuth();
}
}
Do you also export the GlobalService within the GlobalModule as you are importing the GlobalService in your FileService? I think the FileModule itself seems to be correct. I have faced such an issue myself :-) And I don't think you need to import the GlobalService into your FileModule too.
@Module({
imports: [],
components: [],
controllers: [],
exports: [GlobalService],
})
@lo78cn
I'm exporting the GlobalModule which has GlobalService in it, what do you mean export the GlobalService?
I am using the GlobalService in another module (which works) so I am pretty sure there is no problem with it.
The error above appears only when I add the following code to the constructor:
@InjectRepository(File) private readonly fileRepository: Repository<File>
global..module.ts
import { Module } from '@nestjs/common';
import {GlobalService} from "./global.service";
import {GlobalController} from "./global.controller";
@Module({
imports: [],
components: [GlobalService],
controllers: [GlobalController],
})
export class GlobalModule {}
If I don't export the service explicitly, I also get this error message in my other module service. But maybe it is something else.
Error: Nest can't resolve dependencies of the AuthService (?). Please verify whether [0] argument is available in the current context.
Your module could look like:
@Module({
imports: [],
components: [GlobalService],
controllers: [GlobalController],
exports: [GlobalService],
})
export class GlobalModule {}
@lo78cn The issue happens when I add the FileRepository to the constructor, there is nothing wrong about the GlobalService, even when I remove it from the constructor the error keep showing.
Ok, good to know. Based on your code snippets, I can't find any issues. My code looks more or less the same and works fine.
app.module.ts
@Module({
imports: [
UserModule,
TypeOrmModule.forRoot({
type: 'mysql',
...
entities: ['./**/**.entity{.ts,.js}'],
synchronize: false,
}),
],
})
export class ApplicationModule {}
user.module.ts
@Module({
imports: [
TypeOrmModule.forFeature([User]),
],
components: [UserService],
controllers: [UserController],
exports: [
UserService,
],
})
export class UserModule {}
user.service.ts
constructor(
@InjectRepository(User)
private readonly userRepository: Repository<User>,
) {}
Hi,
In file.module.ts
remove GlobalService
from components array (you don't need to define as component because you importing GlobalModule
and service already registered there)
In global.module.ts
make sure you export GlobalService
ex.: exports: [GlobalService]
Thanks @darioxtx, I did this change - but the erro remains.
As I mentioned earlier, the error occurs when I put
@InjectRepository(File) private readonly fileRepository: Repository<File>
in my constructor.
My GlobalModule and service works just fine.
Any idea why I get the following error when I'm injecting the File repository ?
[Nest] 14080 - 2018-2-20 16:48:13 [ExceptionHandler] Nest can't resolve dependencies of the FileService (?, +). Please verify whether [0] a
rgument is available in the current context.
Unfortunately, it seems that we need more context (repository)
@kamilmysliwiec Repo with a reproduction:
@theunreal you're trying to use FilesService
2 times. Remove it from there:
https://github.com/theunreal/nestjs-repro/blob/master/src/bug/bug.module.ts
@kamilmysliwiec but I do use the FileService in my bug module, removing it outputs the following error:
Nest can't resolve dependencies of the BugService (+, +, ?). Please verify whether [2]
argument is available in the current context.
If there is no way to use the FileService in my BugModule, what is the correct way to call methods from one module to another?
exports: [FileService]
in FileModule, import FileModule in BugModule and remove FileService from components in BugModule
if I do it like this, I am able to npm start
the project without any issues
@Module({
imports: [TypeOrmModule.forFeature([File]), GlobalModule],
components: [FileService],
controllers: [FileController],
exports: [FileService]
})
export class FileModule {}
@Module({
imports: [TypeOrmModule.forFeature([Bug]), GlobalModule, FileModule],
components: [BugService, GlobalService],
controllers: [BugController],
})
export class BugModule {}
@lo78cn That's the issue, I had to export FileService and I didn't. Now everything works great.
Thanks!
please check your app.module.ts for the controllers and providers arrays. i was using the cli to generate all the different elements and by default these arrays are updated with the new 'components'. problem is that once you create your own custom module you include these arrays and their content inside. and they are only visible in that specific scope. so if for example you have UserService element inside a services folder inside your model then this elemet is hidden from the global scope.
In my opinion its a bit fkd up because of circular dependency injection and the entire hirarcy Nestjs is build upon or maybe i am mistaken to think it this way, but heck, it solved my issue than i'm glad
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
Do you also export the GlobalService within the GlobalModule as you are importing the GlobalService in your FileService? I think the FileModule itself seems to be correct. I have faced such an issue myself :-) And I don't think you need to import the GlobalService into your FileModule too.