my user.module.ts:
import { forwardRef, Module } from '@nestjs/mon';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import { UserSchema } from './schemas/user.schema';
import { MongooseModule } from '@nestjs/mongoose';
import { AuthModule } from 'src/auth/auth.module';
@Module({
imports: [MongooseModule.forFeature([{name: 'User', schema: UserSchema}]),AuthModule],
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService]
})
export class UsersModule {}
my auth.module.ts
import { forwardRef, Module } from '@nestjs/mon';
import { AuthService } from './auth.service';
import { UsersModule } from '../users/users.module';
import { PassportModule } from '@nestjs/passport';
import { LocalStrategy } from './local.strategy';
@Module({
imports: [forwardRef(() => UsersModule), PassportModule],
providers: [AuthService, LocalStrategy],
})
export class AuthModule {}
my app.module.ts
import { Module } from '@nestjs/mon';
import { MongooseModule } from '@nestjs/mongoose';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from './users/users.module';
@Module({
imports: [UsersModule,MongooseModule.forRoot('mongodb://localhost:27017/nest1')],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
this are giving me error:
[12:55:42 pm] Found 0 errors. Watching for file changes.
[Nest] 15944 - 20/09/2021, 12:55:45 pm LOG [NestFactory] Starting Nest application... [Nest] 15944 - 20/09/2021, 12:55:45 pm LOG [InstanceLoader] MongooseModule dependencies initialized +126ms [Nest] 15944 - 20/09/2021, 12:55:46 pm ERROR [ExceptionHandler] Nest can't resolve dependencies of the UsersService (UserModel, ?). Please make sure that the argument AuthService at index [1] is available in the UsersModule context.
Potential solutions:
- If AuthService is a provider, is it part of the current UsersModule?
- If AuthService is exported from a separate @Module, is that module imported within UsersModule? @Module({ imports: [ /* the Module containing AuthService */ ] })
Error: Nest can't resolve dependencies of the UsersService (UserModel, ?). Please make sure that the argument AuthService at index [1] is available in the UsersModule context.
Potential solutions:
If AuthService is a provider, is it part of the current UsersModule?
If AuthService is exported from a separate @Module, is that module imported within UsersModule? @Module({ imports: [ /* the Module containing AuthService */ ] })
at Injector.lookupComponentInParentModules (C:\Users\Asus\Desktop\BILDEMP\ems-by-nestjs\employee-management-system\node_modules@nestjs\core\injector\injector.js:193:19) at Injector.resolveComponentInstance (C:\Users\Asus\Desktop\BILDEMP\ems-by-nestjs\employee-management-system\node_modules@nestjs\core\injector\injector.js:149:33) at resolveParam (C:\Users\Asus\Desktop\BILDEMP\ems-by-nestjs\employee-management-system\node_modules@nestjs\core\injector\injector.js:103:38) at async Promise.all (index 1) at Injector.resolveConstructorParams (C:\Users\Asus\Desktop\BILDEMP\ems-by-nestjs\employee-management-system\node_modules@nestjs\core\injector\injector.js:118:27) at Injector.loadInstance (C:\Users\Asus\Desktop\BILDEMP\ems-by-nestjs\employee-management-system\node_modules@nestjs\core\injector\injector.js:47:9) at Injector.loadProvider (C:\Users\Asus\Desktop\BILDEMP\ems-by-nestjs\employee-management-system\node_modules@nestjs\core\injector\injector.js:69:9) at async Promise.all (index 3) at InstanceLoader.createInstancesOfProviders (C:\Users\Asus\Desktop\BILDEMP\ems-by-nestjs\employee-management-system\node_modules@nestjs\core\injector\instance-loader.js:44:9) at C:\Users\Asus\Desktop\BILDEMP\ems-by-nestjs\employee-management-system\node_modules@nestjs\core\injector\instance-loader.js:29:13
my user.module.ts:
import { forwardRef, Module } from '@nestjs/mon';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import { UserSchema } from './schemas/user.schema';
import { MongooseModule } from '@nestjs/mongoose';
import { AuthModule } from 'src/auth/auth.module';
@Module({
imports: [MongooseModule.forFeature([{name: 'User', schema: UserSchema}]),AuthModule],
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService]
})
export class UsersModule {}
my auth.module.ts
import { forwardRef, Module } from '@nestjs/mon';
import { AuthService } from './auth.service';
import { UsersModule } from '../users/users.module';
import { PassportModule } from '@nestjs/passport';
import { LocalStrategy } from './local.strategy';
@Module({
imports: [forwardRef(() => UsersModule), PassportModule],
providers: [AuthService, LocalStrategy],
})
export class AuthModule {}
my app.module.ts
import { Module } from '@nestjs/mon';
import { MongooseModule } from '@nestjs/mongoose';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from './users/users.module';
@Module({
imports: [UsersModule,MongooseModule.forRoot('mongodb://localhost:27017/nest1')],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
this are giving me error:
Share Improve this question edited Sep 20, 2021 at 8:03 Jessy_521 asked Sep 20, 2021 at 7:30 Jessy_521Jessy_521 111 silver badge5 bronze badges 1[12:55:42 pm] Found 0 errors. Watching for file changes.
[Nest] 15944 - 20/09/2021, 12:55:45 pm LOG [NestFactory] Starting Nest application... [Nest] 15944 - 20/09/2021, 12:55:45 pm LOG [InstanceLoader] MongooseModule dependencies initialized +126ms [Nest] 15944 - 20/09/2021, 12:55:46 pm ERROR [ExceptionHandler] Nest can't resolve dependencies of the UsersService (UserModel, ?). Please make sure that the argument AuthService at index [1] is available in the UsersModule context.
Potential solutions:
- If AuthService is a provider, is it part of the current UsersModule?
- If AuthService is exported from a separate @Module, is that module imported within UsersModule? @Module({ imports: [ /* the Module containing AuthService */ ] })
Error: Nest can't resolve dependencies of the UsersService (UserModel, ?). Please make sure that the argument AuthService at index [1] is available in the UsersModule context.
Potential solutions:
If AuthService is a provider, is it part of the current UsersModule?
If AuthService is exported from a separate @Module, is that module imported within UsersModule? @Module({ imports: [ /* the Module containing AuthService */ ] })
at Injector.lookupComponentInParentModules (C:\Users\Asus\Desktop\BILDEMP\ems-by-nestjs\employee-management-system\node_modules@nestjs\core\injector\injector.js:193:19) at Injector.resolveComponentInstance (C:\Users\Asus\Desktop\BILDEMP\ems-by-nestjs\employee-management-system\node_modules@nestjs\core\injector\injector.js:149:33) at resolveParam (C:\Users\Asus\Desktop\BILDEMP\ems-by-nestjs\employee-management-system\node_modules@nestjs\core\injector\injector.js:103:38) at async Promise.all (index 1) at Injector.resolveConstructorParams (C:\Users\Asus\Desktop\BILDEMP\ems-by-nestjs\employee-management-system\node_modules@nestjs\core\injector\injector.js:118:27) at Injector.loadInstance (C:\Users\Asus\Desktop\BILDEMP\ems-by-nestjs\employee-management-system\node_modules@nestjs\core\injector\injector.js:47:9) at Injector.loadProvider (C:\Users\Asus\Desktop\BILDEMP\ems-by-nestjs\employee-management-system\node_modules@nestjs\core\injector\injector.js:69:9) at async Promise.all (index 3) at InstanceLoader.createInstancesOfProviders (C:\Users\Asus\Desktop\BILDEMP\ems-by-nestjs\employee-management-system\node_modules@nestjs\core\injector\instance-loader.js:44:9) at C:\Users\Asus\Desktop\BILDEMP\ems-by-nestjs\employee-management-system\node_modules@nestjs\core\injector\instance-loader.js:29:13
-
I mean, I have no experience with nest.js but the error specifically says, "Please make sure that the argument AuthService at index [1] is available in the UsersModule context" so put
AuthService
somewhere in your user.module.ts. Probably in theproviders
array after theUsersService
– Nano Miratus Commented Sep 20, 2021 at 7:36
3 Answers
Reset to default 5I'm going to make some assumptions about your service constructors, as you haven't provided those:
You have what looks to be a circular reference between the UserModule
and the AuthModule
. Because of this, in the imports
of each module you need to use forwardRef
of the module you're importing.
@Module({
imports: [forwardRef(() => UserModule), ...rest],
providers,
controllers,
exports
})
export class AuthModule {}
@Module({
imports: [forwardRef(() => AuthModule), ...rest],
providers,
controllers,
exports
})
export class UserModule {}
And speaking of exports
above, you need to export a provider if you plan to use it outside of the module where it is in the providers
(e.g. to use AuthService
inside of UserService
, the AuthModule
needs to have providers: [AuthService]
and exports: [AuthService]
. This makes the provider available elsewhere.
Now, to make use of circular dependencies inside of the services, you also need to make use of forwardRef
. In this case it will look like @Inject(forwardRef(() => OtherClass)) private readonly other: OtherClass
, so you'd have something like
@Injectable()
export class UserService {
constructor(
@Inject(forwardRef(() => AuthService)) private readonly auth: AuthService,
private readonly dep2: DependencyClass2
) {}
// rest of values
}
You'll do the same thing in the AuthService
but replacing AuthService
in the @Inject()
with UserService
.
You need to import all the dependencies of UserService in UserModule. If you still can't figure out then please also add your user.service.ts file in the question.
You have to export the auth service in the authmodule in order to be able to inject it in the user service.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744756732a4591927.html
评论列表(0条)