Reputation: 329
I am following a tutorial by Wes Bos, Learn Node.js.
I downloaded all the starter files and created a database with MongoDb Atlas.
When I run npm start I get this error:
UnhandledPromiseRejectionWarning: Error: URI does not have hostname, domain name and tld
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.
I am using an environment variable to connect to the database
mongoose.connect(process.env.DATABASE);
and the variable looks like this:
DATABASE=mongodb+srv://<username>:<password>@cluster1-rgvum.mongodb.net/test?retryWrites=true
I omitted my username and password for obvious reasons. If anyone has any idea how to proceed it would be much appreciated! Thank you.
Upvotes: 3
Views: 6820
Reputation: 161
Faced the same issues. It´s not URL encoded...
FAST solution The best case to create a secure password is to use the "Autogenerate a secure password" function in cloud.mongodb.com to match the requirements.
Upvotes: 1
Reputation: 81
Yeah It is because of using special characters in passwords ,just use this simple method either using the above complicated codes for encoding.
In the Password use Hex code instead of symbols which you are using in password like if your password in text is:- pass123#
so use it like:- pass123%23
Get ASCII codes from here and use hex code:- https://ascii.cl/
For more detailed information:- https://docs.atlas.mongodb.com/troubleshoot-connection/#special-characters-in-connection-string-password
Upvotes: 8
Reputation: 2324
This happens because your password is not url encoded. Mongo connection requires password to be url-encoded. Just try adding {useNewUrlParser: true} to your connect method. See below:
mongoose.connect(keys.mongoURI, {useNewUrlParser: true}, (err, db) => {});
Upvotes: 0
Reputation: 892
It has to do with special characters inside your URI (probably your password).
Try this:
"mongodb+srv://<username>:" + encodeURIComponent(<password>) + "@cluster1-rgvum.mongodb.net/test?retryWrites=true"
Upvotes: 3
Reputation: 3849
Make sure your username and password don't contain special characters. It will break the parsing.
This is what was causing me to encounter the same error.
Upvotes: 3
Reputation: 1073
Try adding quotes:
DATABASE="mongodb+srv://<username>:<password>@cluster1-rgvum.mongodb.net/test?retryWrites=true"
Upvotes: 0