英文:
Nest can't resolve dependencies of the ClientsService (?)
问题
Nest无法解析ClientsService的依赖项。请确保在ClientsModule上下文中可用索引[0]处的参数ClientModel可用。
我的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>) { }
// 获取所有客户端
async getClients(): Promise<Client[]> {
const clients = await this.clientModel.find().exec();
return clients
}
// 获取单个客户端
async getClient(clientID: Promise<Client>) {
const client = await this.clientModel
.findById(clientID)
.exec();
return client;
}
// 添加客户端
async addClient(createClientDTO: CreateClientDTO): Promise<Client> {
const newClient = await new this.clientModel(createClientDTO);
return newClient.save()
}
}
我的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 {}
英文:
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 {}
答案1
得分: 1
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.
英文:
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>) { }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论