'MongoParseError: URI does not have hostname, domain name and tld
I'm getting this error when I try to connect my app(nodejs) to mongodb. I really appreciate your help here.
MongoParseError: URI does not have hostname, domain name and tld
at parseSrvConnectionString (E:\Projects\NodeAPI\node_modules\mongodb-core\lib\uri_parser.js:41:21)
at parseConnectionString (E:\Projects\NodeAPI\node_modules\mongodb-core\lib\uri_parser.js:509:12)
at connect (E:\Projects\NodeAPI\node_modules\mongodb\lib\operations\mongo_client_ops.js:195:3)
at connectOp (E:\Projects\NodeAPI\node_modules\mongodb\lib\operations\mongo_client_ops.js:284:3)
at executeOperation (E:\Projects\NodeAPI\node_modules\mongodb\lib\utils.js:416:24)
at MongoClient.connect (E:\Projects\NodeAPI\node_modules\mongodb\lib\mongo_client.js:175:10)
at Function.MongoClient.connect (E:\Projects\NodeAPI\node_modules\mongodb\lib\mongo_client.js:341:22)
at Object.<anonymous> (E:\Projects\NodeAPI\server.js:12:13)
at Module._compile (internal/modules/cjs/loader.js:816:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
name: 'MongoParseError',
[Symbol(mongoErrorContextSymbol)]: {} }
My code:
db.js
module.exports = {
uri : "mongodb+srv://mithun:*******@cluster0-s089x.mongodb.net/test?retryWrites=true"}
==================================================================
node_route.js
module.exports = function(app, db){
app.post('/notes', (req, res) => {
const note = {text: req.body.body, title: req.body.title};
db.collection('notes').insert(note, (err, results) => {
if(err){
res.send({'error': 'An error has occured'});
} else {
res.send(result.ops[0]);
}
});
});
};
======================================================================= index.js
const noteRoutes = require('./note_route');
module.exports = function(app, db){
noteRoutes(app, db);
}
========================================================================= server.js
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const bodyParser = require('body-parser');
const db = require('./config/db');
const app = express();
const port = 8000;
app.use(bodyParser.urlencoded({extended: true}));
MongoClient.connect(db.uri,{ useNewUrlParser: true }, (err, database) =>
{
if (err) return console.log(err);
require('./app/routes')(app, database);
app.listen(port, () => {
console.log("We are live on " +port);
});
});
===========================================================================
module.exports = {
uri : "mongodb+srv://mithun:m3Thun#[email protected]/test?retryWrites=true&ssl=false"
}
I've tried with ssl= false but the error remains same.
Solution 1:[1]
Solution-1: If you are using any special character in your password you need to encode the particular character as %+ASCII_code_of_the_character
below link explains everything.
https://docs.atlas.mongodb.com/troubleshoot-connection/#special-characters-in-connection-string-password
Solution-2: Click on auto-generate password and paste it into the connection string, it will work.
Solution 2:[2]
Ensure special characters in your password are encoded. For example if your password contains '#' you should replace the literal '#' character with '%23' where 23 is the hex code for '#' see: https://docs.atlas.mongodb.com/troubleshoot-connection/#special-characters-in-connection-string-password
Solution 3:[3]
You might have special characters in your password string.
E.g. If your password was - password!#
, !#
are special characters, they would have to be rewritten as %23%21
. Thus, your connection string password should be password%23%21
.
Solution 4:[4]
use '%23' instead of # in your password and
mongoURI = "mongodb+srv://username:[email protected]/dbname?retryWrites=true&w=majority"
mongoose .connect(mongoURI, { useNewUrlParser: true }) .then(() => console.log("MongoDB connected")) .catch((err) => console.log(err));
Solution 5:[5]
Use of the +srv connection string modifier automatically sets the ssl option to true for the connection. You can override this behavior by explicitly setting the ssl option to false with ssl=false in the query string.
May it can be a ssl problem? Try with no ssl: ssl=false
Solution 6:[6]
I also faced this problem.
In my case, I was using npm dotenv
package to get the username the password from a .env
file.
When I logged the values from the .env
file, I noticed it was not retrieving the password correctly.
It could be an issue with either the username or password.
Solution 7:[7]
try to encode your password
ex)
# before encode
test@#123%^
# after encode
test%40%23123%25%5E
this work for me :)
Solution 8:[8]
Convert the special characters (: / ? # [ ] @
) in your password
or username
to percentange characters i.e
@
=%40
'
=%27
!
=%21
...
e.g. p@ssw0rd'9'!
becomes p%40ssw0rd%279%27%21
Solution 9:[9]
Make sure that the username password is character encoded if you have special characters in it like @!: etc or used MongoDB password generator See: https://docs.mongodb.com/manual/reference/connection-string/#examples
Solution 10:[10]
This issue comes if your MongoDB User contains any special characters Try removing any special character from the password
Solution 11:[11]
In my case, it turns out I had a space in the connection string, which was hard to see in the small text input field that Heroku provides for config parameters.
Solution 12:[12]
Even I faced the same issue and I did these steps it got worked for me!
- Goto your monogoDB database (website).
- Change the user password to automatically generated password.
- Paste that auto generated password in the URI string.
- Check the URI once and twice and confirm it and run the app in shell.
Solution 13:[13]
I think your password may contain & or ? (or may special characters) so u should remove it, cause parsing error
Solution 14:[14]
I Resolved it by these steps in MongoDB Compass:-
- Click On "Fill in connection fields individually"
- fill your Username and password
Click on Connect
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow