Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 382
Shyam Avatar asked Oct 15 '25 19:10

Shyam


2 Answers

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

like image 101
Diego G. Teixeira Avatar answered Oct 17 '25 09:10

Diego G. Teixeira


I solve this error by removing special characters from my password MongoDB User. Hope this works for you

like image 45
Inzamam Ahmad Avatar answered Oct 17 '25 08:10

Inzamam Ahmad