nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.7k stars 7.63k forks source link

"Nest can't resolve dependencies" with NestJS v9 when using InjectModel #11559

Closed sagimann closed 1 year ago

sagimann commented 1 year ago

Is there an existing issue for this?

Current behavior

The following error occurs when trying to implement the tutorial's Cat model for Mongo integration: Nest can't resolve dependencies of the CatService (?). Please make sure that the argument CatModel at index [0] is available in the CatModule context.

Minimum reproduction code

https://codesandbox.io/p/sandbox/cool-silence-cfjplu

Steps to reproduce

  1. go to code sandbox link provided
  2. open a terminal and run yarn start:dev
  3. observe the output error

Code overview below:

app.module:

@Module({
  imports: [
    MongooseModule.forRoot(`mongodb://localhost/db1`, {
      connectionName: 'mydb',
    }),
    CatModule,
  ],
  controllers: [AppController],
  providers: [AppService, AppResolver],
})
export class AppModule {}

cat module (all code together):

@Module({
  imports: [MongooseModule.forFeature([{ name: Cat.name, schema: CatSchema }], 'mydb')],
  providers: [CatService],
})
export class CatModule {}

export type CatDocument = HydratedDocument<Cat>;

@Schema()
export class Cat {
  @Prop()
  name: string;

  @Prop()
  age: number;

  @Prop()
  breed: string;
}

export const CatSchema = SchemaFactory.createForClass(Cat);

@Injectable()
export class CatService {
  constructor(@InjectModel(Cat.name) private catModel: Model<Cat>) {}
}

Expected behavior

The tutorial should work as expected, no errors should occur during startup

Package

Other package

@nestjs/mongoose

NestJS version

9.0.0

Packages versions

{
  "name": "nest-starter",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/mongoose": "^9.2.2",
    "@nestjs/platform-express": "^9.0.0",
    "mongoose": "^7.0.5",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "29.2.4",
    "@types/node": "18.11.18",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "29.3.1",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "29.0.3",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.1.1",
    "typescript": "^4.7.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

16.17.0

In which operating systems have you tested?

Other

[10:03:44 AM] Starting compilation in watch mode...

[10:03:47 AM] Found 0 errors. Watching for file changes.

[Nest] 25330  - 04/25/2023, 10:03:48 AM     LOG [NestFactory] Starting Nest application...
[Nest] 25330  - 04/25/2023, 10:03:48 AM     LOG [InstanceLoader] MongooseModule dependencies initialized +37ms
[Nest] 25330  - 04/25/2023, 10:03:48 AM   ERROR [ExceptionHandler] Nest can't resolve dependencies of the CatService (?). Please make sure that the argument CatModel at index [0] is available in the CatModule context.

Potential solutions:
- Is CatModule a valid NestJS module?
- If CatModel is a provider, is it part of the current CatModule?
- If CatModel is exported from a separate @Module, is that module imported within CatModule?
  @Module({
    imports: [ /* the Module containing CatModel */ ]
  })

Error: Nest can't resolve dependencies of the CatService (?). Please make sure that the argument CatModel at index [0] is available in the CatModule context.

Potential solutions:
- Is CatModule a valid NestJS module?
- If CatModel is a provider, is it part of the current CatModule?
- If CatModel is exported from a separate @Module, is that module imported within CatModule?
  @Module({
    imports: [ /* the Module containing CatModel */ ]
  })

    at Injector.lookupComponentInParentModules (/project/home/sagimann/workspace/node_modules/@nestjs/core/injector/injector.js:247:19)
    at Injector.resolveComponentInstance (/project/home/sagimann/workspace/node_modules/@nestjs/core/injector/injector.js:200:33)
    at resolveParam (/project/home/sagimann/workspace/node_modules/@nestjs/core/injector/injector.js:120:38)
    at async Promise.all (index 0)
    at Injector.resolveConstructorParams (/project/home/sagimann/workspace/node_modules/@nestjs/core/injector/injector.js:135:27)
    at Injector.loadInstance (/project/home/sagimann/workspace/node_modules/@nestjs/core/injector/injector.js:61:13)
    at Injector.loadProvider (/project/home/sagimann/workspace/node_modules/@nestjs/core/injector/injector.js:88:9)
    at /project/home/sagimann/workspace/node_modules/@nestjs/core/injector/instance-loader.js:49:13
    at async Promise.all (index 3)
    at InstanceLoader.createInstancesOfProviders (/project/home/sagimann/workspace/node_modules/@nestjs/core/injector/instance-loader.js:48:9)
micalevisk commented 1 year ago

you missed the connection name at @InjectModel() as the docs shows here: https://docs.nestjs.com/techniques/mongodb#multiple-databases

Please use our Discord server next time.