Reputation: 193
I got error like this:
Error: Nest can't resolve dependencies of the UsersService (?). Please make sure that the argument Usersinterface at index [0] is available in the UsersModule context.
Potential solutions:
- If Usersinterface is a provider, is it part of the current UsersModule?
- If Usersinterface is exported from a separate @Module, is that module imported
within UsersModule?
@Module({
imports: [ /* the Module containing Usersinterface */ ]
})
I got many answers for this problem but that answers are little different from my problem.In my issue i got error on interface rather than any controller or service.
Here is the code of usersinterface:
import * as mongoose from 'mongoose'
export interface Usersinterface extends mongoose.Document {
readonly username: string;
readonly password: string;
}
Here is the code of Usersmodule:
@Module({
imports:[],
providers: [UsersService],
controllers: [UsersController]
})
export class UsersModule {}
Here is the code of appmodule:
@Module({
imports: [AppModule,UsersModule,MongooseModule.forRoot("mongodb://localhost:27017/test",{ useNewUrlParser: true })],
controllers: [AppController],
providers: [AppService]
})
export class AppModule {}
Here is the code of UsersService:
@Injectable()
export class UsersService {
private hashLength = 16;
constructor(@Inject('Usersinterface') private readonly userModel:Model<Usersinterface>) {}
Upvotes: 0
Views: 1192
Reputation: 70201
As I just answered in your other question you need to give the module the correct context of what providers are available. To do this, you need to add MongooseMOdule.forFeature([schemaObject])
to your UsersModule
's imports
array. In the end, it should look like this:
@Module({
imports: [MongooseModule.forFeautre([{ name: 'Usersinterface', schema: UsersSchema }]) // you need to create the UsersSchema,
controllers: [UsersController],
providers: [UsersService],
})
export class UsersModule {}
@Injectable()
export class UsersService {
private hasLength = 16;
// make sure this string matches the value in the MongooseModule.forFeature's name property
constructor(@InjectModel('Usersinterface') private readonly userModel: Model<UsersInterface>) {}
Upvotes: 1
Reputation: 6488
There is a problem with the dependency injection and not with your interface. Although you haven't posted the code of your app.component.ts file, I think, it is very likely that you have a constructor like
constructor(private userService: UserService) {}
with the UserService injected. The problem is, that you have the UserService not provided in the AppModule but in the UserModule. Please provide the service in the AppModule instead or inject the UserService only in controllers that you declare in the UserModule (which is probably the best way).
Upvotes: 0