Nest 无法解析 JobsService (?) 的依赖关系。请确保索引 [0] 处的参数 JobModel 在 AppModule 上下文中可用
Posted
技术标签:
【中文标题】Nest 无法解析 JobsService (?) 的依赖关系。请确保索引 [0] 处的参数 JobModel 在 AppModule 上下文中可用【英文标题】:Nest can't resolve dependencies of the JobsService (?). Please make sure that the argument JobModel at index [0] is available in the AppModule context 【发布时间】:2021-03-10 23:53:27 【问题描述】:我真的很困惑,我遇到了这个奇怪的错误,我不知道。
错误:Nest 无法解析 JobsService (?) 的依赖项。请确保索引 [0] 处的参数 JobModel 在 AppModule 上下文中可用。
app.module.ts
import Module from '@nestjs/common';
import AppController from './app.controller';
import AppService from './app.service';
import MongooseModule from '@nestjs/mongoose';
import JobsModule from './jobs/jobs.module';
import JobsService from './jobs/jobs.service';
import JobsController from './jobs/jobs.controller';
import config from './config/config';
@Module(
imports: [JobsModule, MongooseModule.forRoot(config.mongoURI,
useFindAndModify: false,
)],
controllers: [AppController, JobsController],
providers: [AppService, JobsService],
)
export class AppModule
/jobs/jobs.module.ts
import Module from '@nestjs/common';
import MongooseModule from '@nestjs/mongoose';
import JobsService from './jobs.service';
import JobsController from './jobs.controller';
import JobsSchema from './schemas/jobs.schema';
@Module(
imports: [MongooseModule.forFeature([ name: 'Job', schema: JobsSchema ])],
controllers: [JobsController],
providers: [JobsService],
)
export class JobsModule
/job/job.controller.ts
import Controller, Get, Post, Put, Delete, Body, Param from '@nestjs/common';
import JobsService from './jobs.service';
import IJobs from './interfaces/jobs.interface';
@Controller('jobs')
export class JobsController
constructor(private readonly jobsService: JobsService)
@Get()
findAll():Promise<IJobs[]>
return this.jobsService.findAll();
/job/jobs.service.ts
import Injectable from '@nestjs/common';
import InjectModel from '@nestjs/mongoose';
import Model from 'mongoose';
import IJobSchema from './schemas/jobs.schema';
import IJobs from './interfaces/jobs.interface';
@Injectable()
export class JobsService
constructor(@InjectModel('Job') private readonly JobsModel: Model<IJobSchema>)
async findAll():Promise<IJobs[]>
return await this.JobsModel.find();
【问题讨论】:
Might help to go over the page of common errors 【参考方案1】:你已经在你的 JobModule 中声明了 JobsService,从 Appmodule 提供者中删除它
import Module from '@nestjs/common';
import AppController from './app.controller';
import AppService from './app.service';
import MongooseModule from '@nestjs/mongoose';
import JobsModule from './jobs/jobs.module';
import JobsService from './jobs/jobs.service';
import JobsController from './jobs/jobs.controller';
import config from './config/config';
@Module(
imports: [JobsModule, MongooseModule.forRoot(config.mongoURI,
useFindAndModify: false,
)],
controllers: [AppController, JobsController],
providers: [AppService], // Here..
)
export class AppModule
【讨论】:
以上是关于Nest 无法解析 JobsService (?) 的依赖关系。请确保索引 [0] 处的参数 JobModel 在 AppModule 上下文中可用的主要内容,如果未能解决你的问题,请参考以下文章
Nest.js 在单元测试中无法解析 Mongoose 模型依赖
Nest 无法解析 UsersService (UserModel, ?) 的依赖关系