Home > database >  Error after updgrading @nestjs/mongoose from 9.0.3 to 9.1.0 "Error: Nest can't resolve dep
Error after updgrading @nestjs/mongoose from 9.0.3 to 9.1.0 "Error: Nest can't resolve dep

Time:06-06

After updating @nestjs/mongoose from 9.0.3 to 9.1.0, I encountered following error:

[Nest] 3818  - 06/04/2022, 12:50:13 AM   ERROR [ExceptionHandler] Nest can't resolve dependencies of the WatchlistService (?). Please make sure that the argument PortfolioModel at index [0] is available in the AppModule context.

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

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

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

    at Injector.lookupComponentInParentModules (/home/tobias/Github/Financl/financl-api/node_modules/@nestjs/core/injector/injector.js:231:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at Injector.resolveComponentInstance (/home/tobias/Github/Financl/financl-api/node_modules/@nestjs/core/injector/injector.js:184:33)
    at resolveParam (/home/tobias/Github/Financl/financl-api/node_modules/@nestjs/core/injector/injector.js:106:38)
    at async Promise.all (index 0)
    at Injector.resolveConstructorParams (/home/tobias/Github/Financl/financl-api/node_modules/@nestjs/core/injector/injector.js:121:27)
    at Injector.loadInstance (/home/tobias/Github/Financl/financl-api/node_modules/@nestjs/core/injector/injector.js:52:9)
    at Injector.loadProvider (/home/tobias/Github/Financl/financl-api/node_modules/@nestjs/core/injector/injector.js:74:9)
    at async Promise.all (index 4)
    at InstanceLoader.createInstancesOfProviders (/home/tobias/Github/Financl/financl-api/node_modules/@nestjs/core/injector/instance-loader.js:44:9)
    at /home/tobias/Github/Financl/financl-api/node_modules/@nestjs/core/injector/instance-loader.js:29:13
    at async Promise.all (index 1)
    at InstanceLoader.createInstances (/home/tobias/Github/Financl/financl-api/node_modules/@nestjs/core/injector/instance-loader.js:28:9)
    at InstanceLoader.createInstancesOfDependencies (/home/tobias/Github/Financl/financl-api/node_modules/@nestjs/core/injector/instance-loader.js:18:9)
    at /home/tobias/Github/Financl/financl-api/node_modules/@nestjs/core/nest-factory.js:96:17
    at Function.asyncRun (/home/tobias/Github/Financl/financl-api/node_modules/@nestjs/core/errors/exceptions-zone.js:22:13)

This is my setup:

portfolio.entity.ts

export class Portfolio extends Document {
  @Prop({ required: true, trim: true, maxlength: 100, minlength: 1 })
  name: string
}

export const PortfolioSchema = SchemaFactory.createForClass(Portfolio)

database.module.ts

const databaseModules = [
  MongooseModule.forRoot(uris.MONGO_PRIMARY_CONNECTION_STRING, {
    connectionName: 'primary_connection'
  }),
  MongooseModule.forFeature(
    [{ name: Portfolio.name, schema: PortfolioSchema }],
    'primary_connection'
  ),
]

@Module({
  imports: [...databaseModules],
  exports: [...databaseModules]
})
export class DatabaseModule {}

app.module.ts

@Module({
  imports: [DatabaseModule],
  controllers: [
    AccountController
  ],
  providers: [
    AccountService
  ]
})
export class AppModule {}

account.service.ts

@Injectable()
export class AccountService {
  constructor(
    @InjectModel(Portfolio.name) private portfolioModel: Model<Portfolio>
  ) {}
}

package.json

{
  "name": "api-new",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "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 --testTimeout=30000 --runInBand --forceExit",
    "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": "8.4.6",
    "@nestjs/core": "8.4.6",
    "@nestjs/mapped-types": "*",
    "@nestjs/mongoose": "9.1.0",
    "@nestjs/platform-express": "^8.0.0",
    "@nestjs/swagger": "^5.1.4",
    "@types/lodash": "^4.14.181",
    "class-transformer": "^0.4.0",
    "class-validator": "^0.13.1",
    "lodash": "^4.17.21",
    "mongodb-memory-server": "^8.4.2",
    "mongoose": "^6.0.12",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0",
    "swagger-ui-express": "^4.1.6"
  },
  "devDependencies": {
    "@nestjs/cli": "^8.2.4",
    "@nestjs/schematics": "^8.0.0",
    "@nestjs/testing": "^8.0.0",
    "@types/express": "^4.17.13",
    "@types/express-session": "^1.17.4",
    "@types/jest": "^27.0.1",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^4.28.2",
    "@typescript-eslint/parser": "^4.28.2",
    "eslint": "^7.30.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^3.4.0",
    "jest": "^27.0.6",
    "prettier": "^2.3.2",
    "supertest": "^6.1.3",
    "ts-jest": "^27.0.3",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "^3.10.1",
    "typescript": "^4.3.5"
  }
}

CodePudding user response:

You need to supply the connection name to InjectModel

@InjectModel(Portfolio.name, 'primary_connection') private portfolioModel: Model<Portfolio>
  • Related