I want to inject a mongo db model in NestJS Service. The model is present in the root module only.
import { Injectable } from '@nestjs/mon';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { IFolderModel } from './interfaces/folder.interface';
@Injectable()
export class FolderService {
constructor(@InjectModel('IFolder') private folderModel: Model<IFolderModel>) {
}
async create(folderInstance: IFolderModel): Promise<IFolderModel> {
const folderModelToBeSaved = new this.folderModel(folderInstance);
return await folderModelToBeSaved.save();
}
async findAll(): Promise<IFolderModel[]> {
return await this.folderModel.find().exec();
}
}
I am getting following error while starting application using npm run start
Error: Nest can't resolve dependencies of the FolderService (?). Please make sure that the argument at index [0] is available in the AppModule context.
Contents of AppModule
let mongodb = env.mongodb;
let url = `mongodb://${mongodb.user}:${mongodb.pwd}@${mongodb.host}:${mongodb.port}/${mongodb.dbName}`;
@Module({
imports: [MongooseModule.forRoot(url)],
controllers: [AppController, FolderController],
providers: [AppService, FolderService],
})
export class AppModule { }
I want to inject a mongo db model in NestJS Service. The model is present in the root module only.
import { Injectable } from '@nestjs/mon';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { IFolderModel } from './interfaces/folder.interface';
@Injectable()
export class FolderService {
constructor(@InjectModel('IFolder') private folderModel: Model<IFolderModel>) {
}
async create(folderInstance: IFolderModel): Promise<IFolderModel> {
const folderModelToBeSaved = new this.folderModel(folderInstance);
return await folderModelToBeSaved.save();
}
async findAll(): Promise<IFolderModel[]> {
return await this.folderModel.find().exec();
}
}
I am getting following error while starting application using npm run start
Error: Nest can't resolve dependencies of the FolderService (?). Please make sure that the argument at index [0] is available in the AppModule context.
Contents of AppModule
let mongodb = env.mongodb;
let url = `mongodb://${mongodb.user}:${mongodb.pwd}@${mongodb.host}:${mongodb.port}/${mongodb.dbName}`;
@Module({
imports: [MongooseModule.forRoot(url)],
controllers: [AppController, FolderController],
providers: [AppService, FolderService],
})
export class AppModule { }
Share
Improve this question
edited Dec 7, 2018 at 15:51
Kim Kern
60.7k20 gold badges218 silver badges214 bronze badges
asked Dec 6, 2018 at 6:55
Amit DubeAmit Dube
1,0474 gold badges11 silver badges25 bronze badges
2 Answers
Reset to default 6You have to additionally import your schemas with MongooseModule.forFeature
. Usually, you would set up the database connection in the root module and then import the schemas in dedicated feature modules. This is why the imports are separate.
MongooseModule.forFeature([{ name: 'Folder', schema: FolderSchema }])
For further information, see the mongo docs.
This should be in the imports array of your module
@Module({
imports: [
MongooseModule.forFeature([{ name: 'FolderName', schema: FolderSchema }]),
],
})