简体   繁体   English

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

[英]Nest can't resolve dependencies of the ClientsService (?)

So I have a sample app im building in nest js and I hit an error on npm start所以我有一个在 nest js 中构建的示例应用程序,我在 npm 开始时遇到错误

Nest can't resolve dependencies of the ClientsService (?). Nest 无法解析 ClientsService (?) 的依赖关系。 Please make sure that the argument ClientModel at index [0] is available in the ClientsModule context.请确保索引 [0] 处的参数 ClientModel 在 ClientsModule 上下文中可用。

So I have checked it over but cant seem to find why the error is happening所以我已经检查过了,但似乎无法找到错误发生的原因

My client.service.ts我的客户服务.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和我的 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 {}

The InjectModel decorator expects to take the schema name of your entity. InjectModel装饰器期望采用您的实体的模式名称。

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.因此,您在ClientsModule中告诉 mongoose 模式名称是Clients ,但在ClientsService中,您尝试使用模式名称Client注入 model ,这与模块中的契约不同。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM