Depreciation error with UR parser and connection error, incomplete key value pair, mongo

Issue

I’m having a weird issue with Mongo throwing me errors that I can’t seem to resolve. It’s weird, because my code still work. If I hit my end points I get my data, all is great, right? But I’d still rather not have some error hanging over me that can be an issue down the line.

I did do some research on these errors on SO, specifically I found: Unhandled promise rejection: Error: URL malformed, cannot be parsed, and Incomplete key value pair for option remote mongodb, but no solutions that seemed to work.

My Mongo is version 4.0.2. I’m connecting through Mongoose, v5.3.9.

This is the error:

(node:5305) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
connection error { MongoParseError: Incomplete key value pair for option
    at parseQueryString (/Users/jonathankuhl/Documents/Programming/JavaScript/Vue/100daysv3/node_modules/mongodb-core/lib/uri_parser.js:387:13)
    at parseConnectionString (/Users/jonathankuhl/Documents/Programming/JavaScript/Vue/100daysv3/node_modules/mongodb-core/lib/uri_parser.js:447:21)
    at QueryReqWrap.dns.resolveTxt [as callback] (/Users/jonathankuhl/Documents/Programming/JavaScript/Vue/100daysv3/node_modules/mongodb-core/lib/uri_parser.js:127:7)
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:245:10)
  name: 'MongoParseError',
  [Symbol(mongoErrorContextSymbol)]: {} }

Here’s the connection code, most of it pulled and modified directly from the Mongoose getting started guide, as I’ve never used Mongoose before:

const mongoose = require('mongoose');
const user = process.env.MONGO_USER;
const pw = process.env.MONGO_PW;
const uri = `mongodb+srv://${user}:${pw}@cluster0-vji0s.mongodb.net/100days?test?retryWrites=true`;

// wrap the connection in a closure so I can export it with the URI
function Connect(uri) {
    return function() {
        mongoose.connect(uri, {
            auth: {
                user: user,
                password: pw
            }
        });

        const db = mongoose.connection;
        db.on('error', console.error.bind(console, 'connection error'));
        db.once('open', ()=> {
            console.log('\nSuccessfully connected to Mongo!\n');
        });
    }
}

const MongooseConnect = Connect(uri);

module.exports = MongooseConnect;

And in my server (Express JS), I connect once my server is running:

const MongooseConnect = require('./database.js');

//... other express stuff

app.listen(port, ()=> {
    MongooseConnect();
    console.log(`listening on port ${port}\n`)
});

The code works, I connect and hit my data base and do CRUD just fine. I haven’t always gotten this error (well, not the key-pair one, I’ve always had the deprecation warning, and just kicked the can down the road for later), but I have no idea what triggered to change it. The most recent changes I’ve made was to move my schemas to their own folder, I don’t see why that would cause this. I don’t know what causes these error messages to crop up, or what to do to make them go away. Everything I’ve tried in the links I made above don’t work or make things worse.

Any help would be appreciated

Also, my username and password do not use any special characters, only numbers and letters.

Solution

To fix the newUrlParser error you should just have to change your mongoose connect call to:

mongoose.connect(uri, {
    auth: {
        user: user,
        password: pw
    },
    useNewUrlParser: true 
});

I’d see if this fixed the other issue just to be sure.

Edit: I think I know whats causing the other issue

Your connection string is:

mongodb+srv://${user}:${pw}@cluster0-vji0s.mongodb.net/100days?test?retryWrites=true

The portion in question is: /100days?test?retryWrites=true

This is not a proper query string. I am not sure if you were trying to assign a test param, but try this for now /100days?retryWrites=true

If you do need multiple params in a query string, the syntax is /100days?retryWrites=true&secondParam=value&thirdParam=thing

The error was saying the test param has no value, since there is no =value part. It probably threw away the entire query string. So your code should have run as normal, just without the retryWrites=true part being set.

Hope that helped!

Answered By – Brandon Lyons

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published