question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Throw clean error in `openUri()` if uri is `undefined`

See original GitHub issue

Do you want to request a feature or report a bug?

I am trying to get a failing service back up and running. After updating to mongoose@5.2.5 I get the following error message:

UnhandledPromiseRejectionWarning: MongoParseError: URI malformed, cannot be parsed

I’m using mLab so my connection string looks like:

mongodb://<user>:<password>@ds227525.mlab.com:27525/dbname
mongoose.connect(process.env.MONGO_URI, {
  useNewUrlParser: true,
  auth: {
    user: process.env.MONGO_USER,
    password: process.env.MONGO_PASSWORD
  }
})

My question is does auth.user and auth.password get injected into the user/password part of the connection string? Everything I’m trying is still yielding the error message

node 8.11.1 mongoose 5.2.5 mongoDb 4.0.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
lineuscommented, Jul 24, 2018

It seems like the value of process.env.MONGO_URI in your production environment is different from the one in your development environment, possibly being null or undefined.

Is there anyway to return the url so I can see what it looks like? This would be helpful in the debug process.

Have you tried inspecting (console.info, etc ) the value of process.env.MONGO_URI in your production environment right before it’s passed into mongoose.connect()?

Something like the following would ensure it’s set properly before trying to connect:

  try {
    assert.ok(typeof process.env.MONGO_URI === 'string')
  } catch(e) {
    console.error('process.env.MONGO_URI isn't set correctly')
    process.exit(1)
  }

What you are seeing can be replicated by explicitly passing null or undefined (basically anything that isn’t a string, ie {} or 42) as the first argument to mongoose.connect(). All of these values have the same output as null below:

6763.js

#!/usr/bin/env node
'use strict';

const mongoose = require('mongoose');
mongoose.connect(null, { useNewUrlParser: true }).catch(e => {
  console.error(e.message);
});

output:

issues: ./6763.js
URI malformed, cannot be parsed
issues:

My question is does auth.user and auth.password get injected into the user/password part of the connection string?

Mongoose’s connection code will potentially set connection options based on the connection string, but mongoose doesn’t mutate the string based on the options ( or in any way that I’m aware of ).

Just to be sure, I tested mongoose 5.2.5 with my MLab test account and it works without error for me:

6763.js

#!/usr/bin/env node
'use strict';

const { MLAB, sharable, MLAB_USER, MLAB_PASS } = require('/Users/lineus/.env');
const mongoose = require('mongoose');
const conn = mongoose.connection;
const opts = {
  useNewUrlParser: true,
  auth: {
    user: MLAB_USER,
    password: MLAB_PASS 
  }
};
mongoose.connect(MLAB, opts).catch(e => {
  console.error(e.message);
});

const Test = mongoose.model('test', { name: String});

const test = new Test({ name: 'one' });

async function run() {
  console.log(`Mongoose: ${mongoose.version}`);
  console.log(`conn string: ${sharable(MLAB)}`);
  await conn.dropDatabase();
  await test.save();
  let doc = await Test.findOne();
  console.log(doc);
  console.log(sharable.toString());
  return conn.close();
}

run();

Output:

issues: ./6763.js
Mongoose: 5.2.5
conn string: mongodb://user:pass@host.mlab.com:port/dbname
{ _id: 5b57146c9cf05b568a68e8c1, name: 'one', __v: 0 }
function (str) {
    return str.replace(/\/\/[\w]+:/, '//user:')
              .replace(/:[\w]+@/, ':pass@')
              .replace(/@[\w]+\./, '@host.')
              .replace(/(net|com):[0-9]{5}/g, '$1:port')
              .replace(/\/[^/]*$/, '/dbname')
  }
issues:

I included the toString() value of sharable to show how I’m mutating my connection string when I print it out here

Feel free to join us on either Gitter.im or Slack to chat about it in real time.

0reactions
vkarpov15commented, Jan 31, 2020

@chriselliott82 that’s the new error message that we added. Please read me the error message and make sure you aren’t calling mongoose.connect() with no parameters.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The `uri` parameter to `openUri()` must be a string, got ...
When I run my project, I get this error: /home/ali/Desktop/personalitytest-backend/node_modules/mongoose/lib/connection.js:428 throw new ...
Read more >
Mongoose v6.8.2: API docs
If bufferCommands is true, Mongoose will throw an error after bufferTimeoutMS if the operation is still buffered. [options.dbName] «String» The name of the ......
Read more >
MongooseError: The `uri` parameter to `openUri()` must be a ...
This error originated either by throwing inside of an async ... The `uri` parameter to `openUri()` must be a string, got "undefined".
Read more >
Top 10 errors from 1000+ Ruby on Rails projects ... - Rollbar
Table of Contents. Here are the top 10 Rails errors: 1. ActionController::RoutingError; 2. NoMethodError: undefined method '[]' for nil: ...
Read more >
Uri | Android Developers
Behavior is undefined for invalid input. This class is very forgiving--in the face of invalid input, it will return garbage rather than throw...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found