URI does not have hostname, domain name and tld


I am trying to connect VScode program to my cluster but it keeps giving me “URI does not have hostname, domain name and tld” error. I have searched multiple forums with the same problem, all of which are solved with encoding special characters in the password. But my password is auto-generated on Mongodb and doesn’t have any special characters.

This is the code I use to connect to the Mongo database

// Export mongoose
const  mongoose = require("mongoose");

require('dotenv-flow').config();

//Assign MongoDB connection string to Uri and declare options settings
var uri = `mongodb+srv://${process.env.MONGO_URL_NAME}:${process.env.MONGO_PASSWORD}@${process.env.MONGO_URL_CLUSTER}/${process.env.MONGO_DATABASE}?retryWrites=true&w=majority`
console.log(`url: `+uri)
const db = require("../models");
const User = db.user;

db.mongoose
  .connect(uri, {
    useNewUrlParser: true,
    useUnifiedTopology: true
  })
  .then(() => {
    console.log("Successfully connect to MongoDB.");
    initial();
  })
  .catch(err => {
    console.error("Connection error", err);
    process.exit();
  });

function initial() {
	User.estimatedDocumentCount((err, count) => {
	  if (!err && count === 0) {
		new User({
		  email: "admin@domain.com",
		  password: "$2a$08$hVnfdemp6cpovhm0uOvDeOqPcwiO7Ek0SWcGqLwlTTytFRBg7C.TW", // KeepingHumanSafe101
		  accountType: "admin",
		  fname: "Admin",
		  lname: "",
		  accountType: "admin",
		  plan: "Ultimate",
		  status: "active",
		  credits: 10000,
		}).save(err => {
		  if (err) {
			console.log("error", err);
		  }
		  console.log("admin user added");
		});

		new User({
			email: "support@openai.com",
			password: "$2a$08$hVnfdemp6cpovhm0uOvDeOqPcwiO7Ek0SWcGqLwlTTytFRBg7C.TW", // KeepingHumanSafe101
			accountType: "user",
			fname: "OpenAI",
			lname: "Support",
			plan: "Ultimate",
			status: "active",
			credits: 1000,
		  }).save(err => {
			if (err) {
			  console.log("error", err);
			}
			console.log("admin user added");
		  });

	  }
	});
}

Hi @popp_yu

Are you using MongoDB Atlas? And every time?

Creating the uri with mongodb+srv:// is always going to restrict you to the DNS Seed style connection string(Primarily used by Atlas) and would not permit use of standard connection strings. See Connection String URI Format

This could complicate unit testing and any local development against a local mongodb.

Yes Atlas, and yes every time. I have tried using just mongodb:// for standard connection as well but still doesn’t work. Setting the tls and ssl to false in the query string doesn’t seem to work either.

P.S. I am trying to connect to a shared cluster.

It is an error from the parser, so I would definitely suspect the building of the uri string. Without seeing the connection string(sans credentials) it is difficult to diagnose.

Have you used the URI with mongosh or compass, copied directly from the Atlas connect screen?

If that works I would assign the uri directly without using the environment variable interpolation.

I too have this same issue.

I’m guessing we both purchased the same template.

1 Like

Make sure all of your passwords and users are correct. And be sure to command + S to save the inputs. That worked for me.

Your code literally output the line

url: mongodb+srv://MONGO_URL_NAME:MONGO_PASSWORD@MONGO_URL_CLUSTER/...

This means one of 3 things:

1 - your .env is not configured correctly
2 - your .env is not processed correctly, the lines that starts with dotenv-flow: might give some clues
OR
3 - your code does not use the content of your .env correctly

You will need to share your .env and the code that uses your .env.

But first read Formatting code and log snippets in posts.

1 Like

All values are correct. I was able to confirm by connecting through the terminal.

I found the problem with this error.

The issue was having a forward slash in MONGO_URL_CLUSTER.

Originally it was: MONGO_URL_CLUSTER=openai.02ppfro.mongodb.net/myFirstDatabase.
/myFirstDatabase should not be there.

After changes: MONGO_URL_CLUSTER=openai.02ppfro.mongodb.net

This allowed me to connect successfully. But now I have an issue with deploying the frontend.

Thank you for your help.

Hi Chris! Are you able to help me solve this issue please? I am in the setup phase of the project. My MONGO_URL_CLUSTER is set as this:

MONGO_URL_CLUSTER=cluster0.vk9amrl.mongodb.net

So I do not see the ‘/’ issue you are talking about. Was there something else you did to solve this issue? Thanks.