我想在NestJS服务中注入mongo数据库模型。该模型仅存在于根模块中。
<Step
key={item.title}
title={item.title}
icon={item.key !== 1 && item.key === current+1 ? <Icon type="loading" /> : ""}
/>
使用import { Injectable } from '@nestjs/common';
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();
}
}
启动应用程序时出现以下错误
npm run start
AppModule的内容
Error: Nest can't resolve dependencies of the FolderService (?). Please make sure that the argument at index [0] is available in the AppModule context.
答案 0 :(得分:1)
您还必须使用MongooseModule.forFeature
导入模式。通常,您将在根模块中设置数据库连接,然后在专用功能模块中导入模式。这就是为什么进口是分开的。
MongooseModule.forFeature([{ name: 'Folder', schema: FolderSchema }])
有关更多信息,请参见mongo docs。