'Nestjs: Unable to connect mongo asynchronously

I am unable to connect to mongodb asynchronously. Please let me know what am I doing wrong.

MongoConfig file:

import { MongooseModuleOptions } from '@nestjs/mongoose';

export default (): { mongoConfig: MongooseModuleOptions } => ({
  mongoConfig: {
    uri: process.env.DB_URI,
    connectionName: 'learn-nest',
    useCreateIndex: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
    useNewUrlParser: true,
  },
});

Here is the code written inside of App Module

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MongooseModule } from '@nestjs/mongoose';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath: ['.env'],
      isGlobal: true,
      cache: true,
    }),
    MongooseModule.forRootAsync({
      useFactory: (configService: ConfigService): any => {
        configService.get('mongoConfig');
      },
      inject: [ConfigService],
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

This is the error that it throws:

"Cannot destructure property 'retryAttempts' of 'mongooseModuleOptions' as it is undefined"

I do not understand why it's throwing this error because, 'retryAttempts' is an optional property in MongooseModuleOptions interface.

MongooseModuleInterface

export interface MongooseModuleOptions extends ConnectOptions, Record<string, any> {
    uri?: string;
    retryAttempts?: number;
    retryDelay?: number;
    connectionName?: string;
    connectionFactory?: (connection: any, name: string) => any;
}


Solution 1:[1]

If your'e going for async MongoDB configuration, go for a separate config class. I'll explain how:

Make a new service for resolving MongoDB config.

mongodb.config.service.ts

import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import {
  MongooseModuleOptions,
  MongooseOptionsFactory,
} from '@nestjs/mongoose';

@Injectable()
export class MongodbConfigService implements MongooseOptionsFactory {

  constructor(private readonly configService: ConfigService) {}

  //You can retrun promise as well
  public createMongooseOptions(): MongooseModuleOptions {
    return {
      //MONGODB_URL is in .env file
      //MONGO_REPL_SET is in .env file
      //MONGO_AUTH_SOURCE is in .env file
      uri: this.configService.get<string>('MONGODB_URL'),
      useNewUrlParser: true,
      useFindAndModify: false,
      useCreateIndex: true,
      useUnifiedTopology: true,
      replicaSet: this.configService.get<string>('MONGO_REPL_SET'),
      authSource:  this.configService.get<string>('MONGO_AUTH_SOURCE'),
    };
  }
}

Mongoose module import :

//...
MongooseModule.forRootAsync({
      imports: [ConfigModule],
      useClass: MongodbConfigService,
    }),
//...

If you want to configure MongoDB as config namespace :

Make a file for holding the config: mongodb.config.ts

import { registerAs } from '@nestjs/config';

// @returns MongooseModuleOptions
export default registerAs('mongo_db', () => ({
      //MONGODB_URL is in .env file
      //MONGO_REPL_SET is in .env file
      //MONGO_AUTH_SOURCE is in .env file
      uri: process.env.MONGODB_URL,
      useNewUrlParser: true,
      useFindAndModify: false,
      useCreateIndex: true,
      useUnifiedTopology: true,
      replicaSet: process.env.MONGO_REPL_SET,
      authSource: process.env.MONGO_AUTH_SOURCE,
}));

Now in the Config Module import:

import mongoDbConfig from '<path>/mongodb.config';
//...
ConfigModule.forRoot({
      envFilePath: ['.env'],
      isGlobal: true,
      cache: true,
      load:[mongoDbConfig]
    }),
//...

Mongoose module import :

//...
MongooseModule.forRootAsync({
    useFactory:(configService:ConfigService)=>{
       return this.configService.get<MongooseModuleOptions>('mongo_db')
       },
    inject: [ConfigService],
    }),
//...

Both of the approaches should work.

BTW: Replica set and auth source was copied from my config. ignore it if you do not have replica setup.

Solution 2:[2]

Your useFactory doesn't return anything, so there's no options for the MongooseModule to make use of. It would be like doing something like this

function getConfig(configService) {
  config.get('CONFIG_KEY');
}

const config = getConfig(configServce);
console.log(config) // undefined

The factory function doesn't return a value, so it's undefined. You can add return configServicce.get('mongoConfig') and it should work as expected. Or you can make the anonymous arrow function return an object by using () => ({}). The () around the {} are very important for returning an object inline, otherwise JS thinks it's a function body.

Solution 3:[3]

MongooseModule.forRootAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    uri: configService.get<string>('MONGODB_URI'),
  }),
  inject: [ConfigService],
});

Just correct Mongoose dynamic import code to this and replace your MONGO_URI key.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SPS
Solution 2 Jay McDoniel
Solution 3 Nabek Abebe