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");
});
}
});
}
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.
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.
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.