Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 734
Vargis.mithun Avatar asked Apr 18 '19 20:04

Vargis.mithun


1 Answers

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

like image 97
thinclient Avatar answered Oct 06 '22 00:10

thinclient