错误:嵌套无法解析TrackerService的依赖项(?)。请确保AppModule上下文中的索引为[0]的参数DriverModel可用。 enter image description here 可能的解决方案:
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();
}
}
答案 0 :(得分:0)
您可能已将TrackerService
添加到providers
的{{1}}数组中。看到此提供程序已经位于AppModule
的{{1}}数组中,则无需在其他任何地方添加它。如果要在其他地方使用它,则应将其添加到providers
的{{1}}中,然后将TrackerModule
添加到要使用的数组的exports
中TrackerModule
英寸。