Nest 无法解析 ClientsService 的依赖项。

huangapple go评论114阅读模式
英文:

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 &#39;@nestjs/common&#39;;
import { Model } from &#39;mongoose&#39;;
import { InjectModel } from &#39;@nestjs/mongoose&#39;;
import { Client } from &#39;clients/interfaces/client.interface&#39;;
import { CreateClientDTO } from &#39;clients/dto/create-client.dto&#39;;


@Injectable()
export class ClientsService {
    constructor(@InjectModel(&#39;Client&#39;) private readonly clientModel: Model&lt;Client&gt;) { }

    // Get all clients
    async getClients(): Promise&lt;Client[]&gt; {
        const clients = await this.clientModel.find().exec();
        return clients
    }

    //Get single client
    async getClient(clientID: Promise&lt;Client&gt;) {
        const client = await this.clientModel
            .findById(clientID)
            .exec();
        return client;
    }

    //Add client
    async addClient(createClientDTO: CreateClientDTO): Promise&lt;Client&gt; {
        const newClient = await new this.clientModel(createClientDTO);
        return newClient.save()
    }
}

and my client.module.ts

import { Module } from &#39;@nestjs/common&#39;;
import { ClientsService } from &#39;./clients.service&#39;;
import { ClientsController } from &#39;./clients.controller&#39;;
import { MongooseModule } from &#39;@nestjs/mongoose&#39;;
import { ClientSchema } from &#39;clients/schemas/clients.schema&#39;;

@Module({
  imports: [
    MongooseModule.forFeature([{name: &#39;Clients&#39;, 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: &#39;Clients&#39;, schema: ClientSchema}])
constructor(@InjectModel(&#39;Client&#39;) private readonly clientModel: Model&lt;Client&gt;) { }

huangapple
  • 本文由 发表于 2023年1月5日 23:02:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75020449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定