最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to inject model if the model is in the root module only - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 6

You 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 }]),
  ],
})
发布评论

评论列表(0)

  1. 暂无评论