Search code examples
javascriptnode.jstypescriptnestjs

Mongoose Module Unable to connect to the database


Currently I'm learning nestjs, and when I'm trying to connect my nestjs to mongodb, I got a problem like this.

// app.module.ts
@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    MongooseModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (config: ConfigService) => {
        const uri = util.format(
          `mongodb://%s:%s@%s:%d/%s`,
          config.get('DB_USERNAME'),
          config.get('DB_PASSWORD'),
          config.get('DB_HOST'),
          config.get('DB_PORT'),
          config.get('DB_NAME'),
        )
        console.log(uri)
        return {
          uri,
        }
      },
    }),
    UsersModule
  ],
  controllers: [
    AppController
  ],
  providers: [
    AppService
  ],
})
export class AppModule { }

the log of the uri is mongodb://username:password@localhost:27017/the-db

but I always got error

[Nest] 25887  - 11/18/2023, 2:24:05 PM   ERROR [MongooseModule] Unable to connect to the database.

or the full log of the program is like this

[Nest] 25887  - 11/18/2023, 2:24:05 PM     LOG [NestFactory] Starting Nest application...
[Nest] 25887  - 11/18/2023, 2:24:05 PM     LOG [InstanceLoader] MongooseModule dependencies initialized +35ms
[Nest] 25887  - 11/18/2023, 2:24:05 PM     LOG [InstanceLoader] ConfigHostModule dependencies initialized +1ms
[Nest] 25887  - 11/18/2023, 2:24:05 PM     LOG [InstanceLoader] AppModule dependencies initialized +0ms
[Nest] 25887  - 11/18/2023, 2:24:05 PM     LOG [InstanceLoader] ConfigModule dependencies initialized +1ms
[Nest] 25887  - 11/18/2023, 2:24:05 PM     LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
mongodb://username:password@localhost:27017/the-db
[Nest] 25887  - 11/18/2023, 2:24:05 PM   ERROR [MongooseModule] Unable to connect to the database. Retrying (1)...
[Nest] 25887  - 11/18/2023, 2:24:08 PM   ERROR [MongooseModule] Unable to connect to the database. Retrying (2)...
[Nest] 25887  - 11/18/2023, 2:24:11 PM   ERROR [MongooseModule] Unable to connect to the database. Retrying (3)...
[Nest] 25887  - 11/18/2023, 2:24:14 PM   ERROR [MongooseModule] Unable to connect to the database. Retrying (4)...
[Nest] 25887  - 11/18/2023, 2:24:17 PM   ERROR [MongooseModule] Unable to connect to the database. Retrying (5)...
[Nest] 25887  - 11/18/2023, 2:24:20 PM   ERROR [MongooseModule] Unable to connect to the database. Retrying (6)...
[Nest] 25887  - 11/18/2023, 2:24:23 PM   ERROR [MongooseModule] Unable to connect to the database. Retrying (7)...
[Nest] 25887  - 11/18/2023, 2:24:26 PM   ERROR [MongooseModule] Unable to connect to the database. Retrying (8)...
[Nest] 25887  - 11/18/2023, 2:24:29 PM   ERROR [MongooseModule] Unable to connect to the database. Retrying (9)...
[Nest] 25887  - 11/18/2023, 2:24:30 PM   ERROR [ExceptionHandler] Authentication failed.
MongoServerError: Authentication failed.
    at Connection.onMessage (/home/damian/projects/youapp/youapp/node_modules/.pnpm/[email protected]/node_modules/mongodb/src/cmap/connection.ts:409:18)
    at MessageStream.<anonymous> (/home/damian/projects/youapp/youapp/node_modules/.pnpm/[email protected]/node_modules/mongodb/src/cmap/connection.ts:239:56)
    at MessageStream.emit (node:events:513:28)
    at processIncomingData (/home/damian/projects/youapp/youapp/node_modules/.pnpm/[email protected]/node_modules/mongodb/src/cmap/message_stream.ts:193:12)
    at MessageStream._write (/home/damian/projects/youapp/youapp/node_modules/.pnpm/[email protected]/node_modules/mongodb/src/cmap/message_stream.ts:74:5)
    at writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at MessageStream.Writable.write (node:internal/streams/writable:337:10)
    at Socket.ondata (node:internal/streams/readable:766:22)
    at Socket.emit (node:events:513:28)

I got authentication failed in the end, so I try to connect the same credentials with the basic nodejs application, and it was connected properly without any error and I could persist data.

  • I've changed the mongoose version and it's nothing but same
  • I've changed the localhost to 127.0.0.1 but nothing changed
  • I've added ?directConnection=true to the uri but still nothing changed

can somebody tell me what's going on here?:(

edit:

for your information I'm using

  • @nestjs/mongoose: ^10.0.2
  • mongoose: ^8.0.1

Solution

  • you should set authSource: 'admin' also in MongoDB URI mongodb://{{username}}:{{password}}@localhost:27017/{{db_name}}?authSource=admin