MongoDB Problems & Exceptions

So I just had a bunch of problems just getting through exercise 1 with Mongoose throwing “unhandled promise” exceptions. I figured I’d write it up.

Turned out that while I was creating my database and database user, I created two problems.

1: Database user… I used a password manager to generate and store a random password. It used a special char (&) that doesn’t work well in URLs. I considered escaping it, but decided to just change the password.

2: I started the database while using my phone as a mobile hotspot during my commute. Continued it while on the company network at lunch.

My connections were failing due to me whitelisting my IP address from the phone, but not my IP on Glitch. I figured even if I got the IP for my app from Glitch, I wasn’t sure that would remain good.

Whitelisting all IP addresses ( is not the best security. MongoDB will even send you a notice to make sure you know what you did. But since this is just for practice and it’s a free tier db, you can be a little more relaxed… so long as you understand what you’re doing, why you’re doing it, and you don’t make a habit of it.

My last problem was remembering to specify the Glitch project properly

3: Remember it’s, not

Hope this helps someone.

The “unhandled promise” exceptions are errors somewhere in your code that are being thrown from a promise chain:

model.find({_id: 'idToFind'})
  .then(data => console.log('data', data));
// The find method could throw and error and this code doesn't catch it
model.find({_id: 'idToFind'})
  .then(data => console.log('data', data))
  .catch(err => console.error(err));
// this could would print out the error to the console, and won't get the unhandled promise exception.

You can also setup node to handle promise errors with the following:

process.on('unhandledRejection', (reason, p) => {

I’m sure there are a number of other ways to handle the error, but you should look into why there is an error and fix it, as its a sign your code is not working as expected :smile: I couldn’t tell you why from what you provided as your essentially providing context for an error that we don’t know about.

Usually when debugging its best to figure out what exactly is wrong before trying to figure out how to fix it, as you could spent tons of time guessing how to fix it when the error is something very simple.

The key context was in the first sentence… “exercise 1.” My code at that point was literally requiring a couple of modules and mongoose.connect(process.env.MONGO_URI);

Opening up the IP whitelist and removing the ampersand from my password fixed the errors. The only code change I made was editing the connection string in my .env file to use the updated password.

Maybe it’s possible I have learned some tricks in the 39 years since I took my first class in computer programming.

For example, before I dive under the hood to correct all the exception handling in someone else’s library, I check my inputs and my environment first, because I not only don’t have the time and energy to fix their edge-case errors, I’m probably causing them.