Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nest server not connecting to MongoDB cloud UnhandledPromiseRejectionWarning: MongoParseError: URI malformed

My NestJS backend needs to connect to the mongodb cloud, I followed the docs from here

The following error threw up in the terminal:

    (node:6920) UnhandledPromiseRejectionWarning: MongoParseError: URI malformed
    at new ConnectionString (D:\growth\quizbackend\quizbackend\node_modules\mongodb-connection-string-url\src\index.ts:113:13)
    at Object.parseOptions (D:\growth\quizbackend\quizbackend\node_modules\mongodb\src\connection_string.ts:249:15)
    at new MongoClient (D:\growth\quizbackend\quizbackend\node_modules\mongodb\src\mongo_client.ts:332:22)
    at D:\growth\quizbackend\quizbackend\node_modules\mongoose\lib\connection.js:785:16
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (D:\growth\quizbackend\quizbackend\node_modules\mongoose\lib\connection.js:782:19)
    at Mongoose.createConnection (D:\growth\quizbackend\quizbackend\node_modules\mongoose\lib\index.js:275:10)
    at Function.<anonymous> (D:\growth\quizbackend\quizbackend\node_modules\@nestjs\mongoose\dist\mongoose-core.module.js:60:63)
    at Generator.next (<anonymous>)
    at D:\growth\quizbackend\quizbackend\node_modules\@nestjs\mongoose\dist\mongoose-core.module.js:20:71
(Use `node --trace-warnings ...` to show where the warning was created)
(node:6920) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:6920) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, 
promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

My app module code:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from './users/users.module';
import {MongooseModule} from '@nestjs/mongoose'
@Module({
  imports: [UsersModule,MongooseModule.forRoot('mongodb+srv://[email protected]/nest-js-db?retryWrites=true&w=majority')],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

I doubled check my username and password but they are correct, is there any need for encoding them or why the error is throwing up any explanation would be appreciated.

like image 587
Samiur Rahaman Khan Avatar asked Oct 27 '25 14:10

Samiur Rahaman Khan


1 Answers

Ensure that your username and password in the URI Connection string doesn't have any illegal characters.

If the username or password includes the following characters:

: / ? # [ ] @

those characters must be converted using percent encoding.

Based on their documentation: https://docs.mongodb.com/manual/reference/connection-string/

like image 188
Lars Flieger Avatar answered Oct 30 '25 04:10

Lars Flieger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!