mongoose Nest无法解析movieModel(?)的依赖项,请确保索引[0]处的参数DatabaseConnection可用,

nr9pn0ug  于 11个月前  发布在  Go
关注(0)|答案(1)|浏览(131)

我有麻烦与使用 Mongoose 在巢
错误消息如下:
Nest无法解析movieModel(?)的依赖项。请确保索引[0]处的参数DatabaseConnection在MongooseModule上下文中可用。
潜在的解决方案:

  • 如果DatabaseConnection是一个提供者,它是当前MongooseModule的一部分吗?
  • 如果DatabaseConnection是从一个单独的@Module导出的,那么该模块是否在MongooseModule中导入?

movies.service.ts

import { Injectable } from '@nestjs/common';
import { CreateMovieDto } from './dto/create-movie.dto';
import { InjectModel } from '@nestjs/mongoose';
import { movie, movieDocument } from './schema/movie.schema';
import { Model } from 'mongoose';

@Injectable()
export class MoviesService {
  constructor(
    @InjectModel(movie.name) private movieModel: Model<movieDocument>,
  ) {}

  async create(CreateMovieDto: CreateMovieDto): Promise<movie> {
    return new this.movieModel(CreateMovieDto).save();
  }

  async getAll(): Promise<movie[]> {
  return this.movieModel.find().exec(); 
  }

字符串
app.modules.ts

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { MoviesModule } from './movies/movies.module';

@Module({
  imports: [
    MoviesModule,
    MongooseModule.forRoot('mongodb://localhost:27017/databasename', {
      connectionName: 'movies',
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}


movie.module.ts

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { MoviesController } from './movies.controller';
import { MoviesService } from './movies.service';
import { movieSchema } from './schema/movie.schema';

@Module({
  imports: [
    MongooseModule.forFeature([
      {
        name: 'movie',
        schema: movieSchema,
        collection: 'movies',
      },
    ]),
  ],
  controllers: [MoviesController],
  providers: [MoviesService], //nestjs가 movieservice를 import하고 controller에 inject 한다.
})
export class MoviesModule {}


请帮我弄清楚

50few1ms

50few1ms1#

确保您的main.ts正在调用NestFactory.create(AppModule)而不是NestFactory.create(MoviesModule)

相关问题