admin管理员组文章数量:1344979
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 }]),
],
})
本文标签: javascriptHow to inject model if the model is in the root module onlyStack Overflow
版权声明:本文标题:javascript - How to inject model if the model is in the root module only - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743751225a2532684.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论