'I'm trying to connect monogoDB with node.js, but the terminal is showing MongoAPIError: URI must include hostname, domain name, and tld

I'm trying to connect MongoDB with node.js, but the terminal is showing MongoAPIError: URI must include hostname, domain name, and tld

  • Backend Code

I have written monogoDB code in a database file and added/required the file path in the backend file

const express = require("express");
const app = express();
const dotenv = require("dotenv");
const path = require('path');
const bcrypt = require('bcrypt');

dotenv.config({path: './config.env'})
require('./database-connection.js')

const port = 80
  • MongoDB Database Code

Database connection file name: database-connection.js


const mongoose = require("mongoose");

const db = mongodb+srv://kumbamshyam:Superman@[email protected]/ecommerce-website?retryWrites=true&w=majority

mongoose.connect(db).then(()=>{ 
    console.log('conencted successfuly');
}).catch((err)=>{ 
    console.log("Error received= " + err)
})
  • <----- Error Code ----->
Error received= MongoAPIError: URI must include hostname, domain name, and tld

Any solution for this problems



Solution 1:[1]

Probably it's because you've used the special character "@" in your password, and a character like this "must be converted using percent encoding" (https://docs.mongodb.com/manual/reference/connection-string/#examples).

Use the percent sign followed by the HEX code of the special character you've used (i.e., the number 40).

So, your password must be written this way: Superman%40123

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 Diego Teixeira