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

时间:2020-09-09 07:49:12

标签: typescript nestjs

错误:嵌套无法解析TrackerService的依赖项(?)。请确保AppModule上下文中的索引为[0]的参数DriverModel可用。 enter image description here 可能的解决方案:

  • 如果DriverModel是提供程序,它是否是当前AppModule的一部分?
  • 如果从单独的@Module导出DriverModel,那么该模块是否在AppModule中导入? @Module({ 导入:[/ *包含DriverModel * /的模块
app.module

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AppGateway } from './app.gateway';
import { TrackerModule } from './tracker/tracker.module';
import { MongooseModule } from '@nestjs/mongoose';

@Module({
  imports: [TrackerModule,MongooseModule.forRoot('mongodb://127.0.0.1:27017/tracker')],
  controllers: [AppController],
  providers: [AppGateway,AppService],
})
export class AppModule {}
'

tracker.module

import { Module } from '@nestjs/common';
import { TrackerService } from './tracker.service';
import { MongooseModule } from '@nestjs/mongoose';
import { DriverSchema } from './schemas/driver.schema';

@Module({
  imports: [MongooseModule.forFeature([{ name :'Driver', schema: DriverSchema }])],
  controllers: [],
  providers: [TrackerService]
})
export class TrackerModule {}


tracker.service

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Driver } from './interfaces/driver.interface';
import { Model } from 'mongoose';
import { driverDto } from './dtos/driver.dto';

@Injectable()
export class TrackerService {
    
    constructor(@InjectModel('Driver') private driverModel: Model<Driver>){}

    addDriver( driverDTO: driverDto): Promise<Driver>{
    const newDriver = new this.driverModel(driverDTO);
    return newDriver.save();
    }
}

1 个答案:

答案 0 :(得分:0)

您可能已将TrackerService添加到providers的{​​{1}}数组中。看到此提供程序已经位于AppModule的{​​{1}}数组中,则无需在其他任何地方添加它。如果要在其他地方使用它,则应将其添加到providers的{​​{1}}中,然后将TrackerModule添加到要使用的数组的exportsTrackerModule英寸。