NestJS : TypeError: Cannot read property 'get' of undefined
Asked Answered
C

4

10

I am trying to pass the connection parameters to mongodb by enviroment file and I am getting the following error

[Nest] 1176  - 12/10/2021 23:34:35   ERROR [ExceptionHandler] Cannot read property 'get' of undefined
TypeError: Cannot read property 'get' of undefined
    at MongoService.createMongooseOptions (/Users/Desarrollos/NestJS/nestjs-ventas-negocios/src/common/mongo/mongo.service.ts:20:41)
    at Function.<anonymous> (/Users/Desarrollos/NestJS/nestjs-ventas-negocios/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:135:120)
    at Generator.next (<anonymous>)
    at /Users/Desarrollos/NestJS/nestjs-ventas-negocios/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:20:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/Desarrollos/NestJS/nestjs-ventas-negocios/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:16:12)
    at InstanceWrapper.useFactory [as metatype] (/Users/Desarrollos/NestJS/nestjs-ventas-negocios/node_modules/@nestjs/mongoose/dist/mongoose-core.module.js:135:45)
    at Injector.instantiateClass (/Users/Desarrollos/NestJS/nestjs-ventas-negocios/node_modules/@nestjs/core/injector/injector.js:294:55)
    at callback (/Users/Desarrollos/NestJS/nestjs-ventas-negocios/node_modules/@nestjs/core/injector/injector.js:43:41)
    at Injector.resolveConstructorParams (/Users/Desarrollos/NestJS/nestjs-ventas-negocios/node_modules/@nestjs/core/injector/injector.js:119:24)

This is the service mongo.service.ts

import { MongooseModuleOptions, MongooseOptionsFactory } from '@nestjs/mongoose';
import { Configuration } from '../../config/config.keys';
import { ConfigService } from '../../config/config.service';

export class MongoService implements MongooseOptionsFactory {
    constructor( private configService: ConfigService ) {}

    createMongooseOptions(): MongooseModuleOptions {

        const user     = this.configService.get(Configuration.DB_MONGO_USER);
        const password = this.configService.get(Configuration.DB_MONGO_PASSWORD);
        const server   = this.configService.get(Configuration.DB_MONGO_HOST);
        const database = this.configService.get(Configuration.DB_MONGO_DATABASE);

        return {
            uri: `mongodb://${user}:${password}@${server}/${database}?retryWrites=true&w=majority`,
        };
    }
}

And this I import it in the app.module.ts

@Module({
  imports: [
    MongooseModule.forRootAsync({
      useClass: MongoService,
    }),

Any suggestion, thanks, JM

Cambria answered 13/10, 2021 at 2:45 Comment(3)
Did you search this site for cannot get property of undefined? There have been literally hundreds (if not thousands) of previous questions related to the same error asked (and answered) here before. Surely one of them can point out a way for you to solve this problem.Thinia
Maybe you forgot to add @Injectable() above MongoServiceMistrustful
adding @Injectable I get the following error : Error: Nest can't resolve dependencies of the MongoService (?). Please make sure that the argument ConfigService at index [0] is available in the MongooseCoreModule context.Cambria
H
10

You need 2 things here:

  1. Your MongoService needs to be marked with @Injectable() so that Nest can read the metadata of the constructor and set up the injection properly

  2. If your ConfigModule does not have a globally exported ConfigService, then in MongooseModule.forRootAsync() along with the useClass you need to have imports: [ConfigModule] so that you can inject the ConfigService

Hibben answered 13/10, 2021 at 15:0 Comment(4)
Thanks Jay, I'm obviously connecting something wrong and I can't see it. With @Injectable() , I get the following error : ERROR [ExceptionHandler] Nest can't resolve dependencies of the MongoService (?). Please make sure that the argument ConfigService at index [0] is available in the MongooseCoreModule context.Cambria
Did you add the ConfigModule to the MongooseModule.forRootAsync() options, like I mentioned in point 2?Hibben
The same error : @Module({ imports: [ MongooseModule.forRootAsync({ imports: [ ConfigService ], useClass: MongoService, inject: [ConfigService], }), VentaNegociosModule, ConfigModule ],Cambria
You have imports: [ConfigService], not imports: [ConfigModule]. You also don't need injectHibben
M
3

@Module({ 
  imports: [ 
    MongooseModule.forRootAsync({ 
      imports: [ ConfigModule ], 
      inject: [ConfigService],
      useClass: MongoService
  })
})
Massacre answered 12/4, 2022 at 8:59 Comment(0)
B
0

After search for several posts, if you are using nx for nestjs and using esbuild for compiler, you might have this problem. The way to make it work is to change back to webpack, because esbuild is having issue with monorepo.

Ref: https://github.com/nrwl/nx/issues/20546

Berner answered 17/1, 2024 at 14:40 Comment(0)
K
0

I think u fotgot add inject, try this:

@Module({
  imports: [
    UsersModule,
    JwtModule.registerAsync({
      useFactory: async (configService: ConfigService) => ({
        secretOrPrivateKey: 
        signOptions: {
      }),
      inject: [ConfigService], // i think u add line this
    }),

  ],
  controllers: [AuthController],
  providers: [AuthService],
})
export class AuthModule { }
Kristykristyn answered 16/8, 2024 at 4:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.