Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoParseError: Unescaped at-sign in authority section at parseConnectionString

I am trying to connect my nodejs application to mongoDB Using mlab but I am getting error which is not understandable for me.

let mongoose=require('mongoose');

const server='ds366482.mlab.com:11275';
const database='customer_db'
const user='xyz'
const password='xyz@5295'


mongoose.connect(`mongodb://${user}:${password}@${server}/${database}`, {useNewUrlParser: true})


let customerSchema=new mongoose.Schema({
    name:String,
    email:{
        type:String,
        required:true,
        unique:true
    }
})

This is the actual result of my code:


server has started on 3000
(node:4800) UnhandledPromiseRejectionWarning: MongoParseError: Unescaped at-sign in authority section
    at parseConnectionString (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/node_modules/mongodb-core/lib/uri_parser.js:450:21)
    at connect (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/node_modules/mongodb/lib/operations/mongo_client_ops.js:180:3)
    at connectOp (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/node_modules/mongodb/lib/operations/mongo_client_ops.js:284:3)
    at executeOperation (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/node_modules/mongodb/lib/utils.js:420:24)
    at MongoClient.connect (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/node_modules/mongodb/lib/mongo_client.js:168:10)
    at Promise (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/node_modules/mongoose/lib/connection.js:521:12)
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/node_modules/mongoose/lib/connection.js:518:19)
    at Mongoose.connect (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/node_modules/mongoose/lib/index.js:270:15)
    at Object.<anonymous> (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/src/models/customer.model.js:9:10)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
(node:4800) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing insideof an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:4800) [DEP0018] 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.
like image 480
Gaurav Kumar Avatar asked Jan 23 '19 21:01

Gaurav Kumar


2 Answers

well, so that is because of the @ in password, it unable to understand which part it should consider as password or host, if you see the Mongo string format:

mongodb://<user>:<pass>@<host>:<port>/<db_name>

here Password and Host is separated by @ and if there is one more @ comes in, say in your case its inside password, Mongo String will look like this:

mongodb://<user>:<pass_1st_part>@<pass_2nd_part>@<host>:<port>/<db_name> which is not acceptable,

for this you can do something like this:

mongoose.connect(`mongodb://<USER_NAME>:${encodeURIComponent('<P@SS>')}@<HOST>:<PORT>/<DB_NAME>`)

Here we are encoding the password, with encodeURIComponent(), so the p@ss will become p%40ss.

You can always replace the @ with %40 but it's not the best way to do it sometimes. so you can use some functions like above for you to do the same.

NOTE: sometimes you may need to add { uri_decode_auth: true } in option. if you are using Mongo Native Driver.

like image 138
Saikat Chakrabortty Avatar answered Nov 10 '22 09:11

Saikat Chakrabortty


Just replace the @ sign by %40. it will solve it.

like image 19
DubaniWarrior Avatar answered Nov 10 '22 08:11

DubaniWarrior