Reputation: 197
So I have a sample app im building in nest js and I hit an error on npm start
Nest can't resolve dependencies of the ClientsService (?). Please make sure that the argument ClientModel at index [0] is available in the ClientsModule context.
So I have checked it over but cant seem to find why the error is happening
My client.service.ts
import { Injectable } from '@nestjs/common';
import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
import { Client } from 'clients/interfaces/client.interface';
import { CreateClientDTO } from 'clients/dto/create-client.dto';
@Injectable()
export class ClientsService {
constructor(@InjectModel('Client') private readonly clientModel: Model<Client>) { }
// Get all clients
async getClients(): Promise<Client[]> {
const clients = await this.clientModel.find().exec();
return clients
}
//Get single client
async getClient(clientID: Promise<Client>) {
const client = await this.clientModel
.findById(clientID)
.exec();
return client;
}
//Add client
async addClient(createClientDTO: CreateClientDTO): Promise<Client> {
const newClient = await new this.clientModel(createClientDTO);
return newClient.save()
}
}
and my client.module.ts
import { Module } from '@nestjs/common';
import { ClientsService } from './clients.service';
import { ClientsController } from './clients.controller';
import { MongooseModule } from '@nestjs/mongoose';
import { ClientSchema } from 'clients/schemas/clients.schema';
@Module({
imports: [
MongooseModule.forFeature([{name: 'Clients', schema: ClientSchema}])
],
providers: [ClientsService],
controllers: [ClientsController]
})
export class ClientsModule {}
Upvotes: 0
Views: 757
Reputation: 3687
The InjectModel
decorator expects to take the schema name of your entity.
So you tell the mongoose in ClientsModule
that the schema name is Clients
, but in ClientsService
you try to inject the model with the schema name Client
, which is different from the contract in the module.
MongooseModule.forFeature([{name: 'Clients', schema: ClientSchema}])
constructor(@InjectModel('Client') private readonly clientModel: Model<Client>) { }
Upvotes: 1