Nest 无法解析 MongooseCoreModule 的依赖关系(MongooseConnectionName,?)

问题描述 投票:0回答:5

Nest can't resolve dependencies of the MongooseCoreModule (MongooseConnectionName, ?). Please make sure that the argument ModuleRef at index [1] is available in the MongooseCoreModule context.

我是嵌套新手,正面临这个问题,app.controller.ts 是

import { Module } from '@nestjs/common';
import { ItemsController } from './items.controller';
import { ItemsService } from './items.service';
import { MongooseModule } from '@nestjs/mongoose';
import { ItemSchema } from './schema/item.schema';

@Module({
  imports: [MongooseModule.forFeature([{name:'Item',schema:ItemSchema}])],
  controllers: [ItemsController],
  providers: [ItemsService],
})
export class ItemsModule {}

如有任何帮助,我们将不胜感激

javascript nestjs
5个回答
1
投票

在您的情况下,您可以尝试将其添加到您的 ItemSchema 中:

// schema/item.schema

@Schema({ collection: 'items' })

并确保导入时使用复数名称:

// items.module.ts

// [...]
@Module({
  imports: [MongooseModule.forFeature([{name:'items',schema:ItemSchema}])],
  controllers: [ItemsController],
  providers: [ItemsService],
})
export class ItemsModule {}

根据这个问题

或者,如果您更喜欢保留名称模式名称

item
,您可以按照以下这个答案

强制它

Mongoose 试图通过将你的集合名称设为复数来变得聪明。但是,您可以强制它成为您想要的任何内容:

var dataSchema = new Schema({..}, { 集合: '数据' })


1
投票

我在学习将 Nest.js 与 Azure Cosmos DB 连接时遇到此错误。 错误:“Nest 无法解析 AzureCosmosDbCoreModule(COSMOS_DB_CONNECTION_NAME,?)的依赖项。请确保索引 [1] 处的参数 ModuleRef 在 AzureCosmosDbCoreModule 上下文中可用。

Potential solutions:
- Is AzureCosmosDbCoreModule a valid NestJS module?
- If ModuleRef is a provider, is it part of the current AzureCosmosDbCoreModule?
- If ModuleRef is exported from a separate @Module, is that module imported within AzureCosmosDbCoreModule?
  @Module({
    imports: [ /* the Module containing ModuleRef */ ]
  })"
Here is the solution: 
Check your dependencies in the Package.json file.

"dependencies": {
    "@azure/cosmos": "^3.17.3",
    "@nestjs/azure-database": "^2.3.0",
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "dotenv": "^16.1.4",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0"
  },
  "overrides": {
    "@nestjs/azure-database": {
      "@nestjs/common": "^9.1.4",
      "@nestjs/core": "^9.1.4"
    } 
This may help you!.

0
投票

当您没有将代码放在程序文件夹的正确目录中时,就会出现此错误。因此,

start
导航到您的项目目录并再次运行所有代码。
    


0
投票


0
投票
cd

并运行

node_modules
。它对我的项目有用。
    

© www.soinside.com 2019 - 2024. All rights reserved.