NodeJS Nest无法解析ClientsService(?)的依赖项

9o685dep  于 2023-01-08  发布在  Node.js
关注(0)|答案(1)|浏览(118)

我有一个用nest js构建的示例应用程序im,在npm启动时遇到了一个错误
Nest无法解析ClientsService(?)的依赖项。请确保索引[0]处的参数ClientModel在ClientsModule上下文中可用。
所以我已经检查了一遍,但似乎找不到错误发生的原因
我的客户服务部

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()
    }
}

以及我的客户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 {}
afdcj2ne

afdcj2ne1#

InjectModel装饰器需要实体的模式名。
因此,您告诉ClientsModule中的mongoose模式名是Clients,但在ClientsService中,您尝试用模式名Client注入模型,这与模块中的契约不同。

MongooseModule.forFeature([{name: 'Clients', schema: ClientSchema}])
constructor(@InjectModel('Client') private readonly clientModel: Model<Client>) { }

相关问题